'How to force UILabel to draw a text with upper case chars?

How to force UILabel to draw a text with upper case chars?



Solution 1:[1]

Objective-C

NSString *text = @"Hello";
[myLabel setText:[text uppercaseString]];

Swift 3 & 4

let text = "Hello"
myLabel.text = text.uppercased()

Solution 2:[2]

You were asking if there might be an equivalent to the CSS declaration text-transform: uppercase; for attributed text in iOS. Unfortunately this is not available, and I agree that it could be convenient to add an attribute for this.

I've personally subclassed UILabel, added an uppercase boolean property, and overridden the text setter so that it will automatically call uppercaseString on the new value if uppercase is set to true.

Solution 3:[3]

This is the Swift 3 & Swift 4 version:

titleLabel.text = titleLabel.text?.uppercased()

Solution 4:[4]

If you wish to force a string to display in all Uppercase characters in Swift here is my code that works great for me.

titleLabel.text = youStringVariable.uppercaseString 

Solution 5:[5]

A quick look into the documentation would have been quicker.

NSString *upperCase = [string uppercaseString];

Solution 6:[6]

Instead of changing the text to be uppercase, you can use an uppercase / small-caps font.

titleLabel.font = .smallCapsSystemFont(ofSize: 17, weight: semibold)
extension UIFont {
    
  static func smallCapsSystemFont(ofSize size: CGFloat, weight: Weight) -> UIFont { 
    // Upper case letters are in small caps
    let upperCaseFeature: [UIFontDescriptor.FeatureKey: Int] = [
      .type: kUpperCaseType,
      .selector: kUpperCaseSmallCapsSelector
    ]

    // Lower case letters are in small caps
    let lowerCaseFeature: [UIFontDescriptor.FeatureKey: Int] = [
      .type: kLowerCaseType,
      .selector: kLowerCaseSmallCapsSelector
    ]

    let features = [upperCaseFeature, lowerCaseFeature]
    let descriptor = UIFont.systemFont(ofSize: size, weight: weight)
      .fontDescriptor
      .addingAttributes([.featureSettings: features])

    return UIFont(descriptor: descriptor, size: size)
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Linus Unnebäck
Solution 2 ndbroadbent
Solution 3 aturan23
Solution 4 CakeGamesStudios
Solution 5 Mundi
Solution 6 amgalan-b