UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to display different strings based on available space using variantFittingPresentationWidth()

Swift version: 5.6

Paul Hudson    @twostraws   

It’s surprisingly easy to configure your project with multiple strings then have it choose one at runtime based on available space.

First, press Cmd+N in Xcode to make a new file, then choose “Stringsdict file” – this is a property list XML file containing string settings. Name it “Localizable.stringsdict”, so that iOS picks it up automatically.

Right-click on the new Localizable.stringsdict file in your Xcode project, then choose Open As > Source Code so you can see the XML inside. You should see that it ends like this:

</dict>
</plist>

Add this new XML directly before those two lines:

<key>Login</key>
<dict>
    <key>NSStringVariableWidthRuleType</key>
    <dict>
        <key>100</key>
        <string>Login.</string>
        <key>200</key>
        <string>You must login before continuing.</string>
        <key>300</key>
        <string>Please enter your username and password to continue.</string>
    </dict>
</dict>

That defines a single string key, “Login”, but provides three size variations: one for very little space (size 100), one for a medium amount of space (size 200), and one for lots of space (size 300). These size integers mean nothing to iOS – you can use any numbers that make sense to you, but increments of 100 leave you lots of space to insert new values in between later on.

Now that you have a width-varying string to work with, you can pass that to NSLocalizedString(). Note that you must cast the result to an NSString:

let localized = NSLocalizedString("Login", comment: "Prompt for user to log in.") as NSString

Finally, call variantFittingPresentationWidth() with a size integer of your choosing:

label.text = localized.variantFittingPresentationWidth(300)

That method only exists on NSString, hence the earlier typecast.

You can pass any integer you want into variantFittingPresentationWidth() – iOS will automatically resolve it to find the best match in your strings dictionary, counting downwards where necessary. For example, if you tried loading a string with width 500, the 300 string would be returned, but if you tried 299 then the 200 string would be returned.

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Available from iOS 9.0

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 3.5/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.