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

6

35

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

Ultra answered 15/9, 2013 at 21:56 Comment(2)
Either what you are really asking for didn't come through very well or you didn't do much research.Wideranging
I hoped to find something like font style.Ultra
C
62

Objective-C

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

Swift 3 & 4

let text = "Hello"
myLabel.text = text.uppercased()
Clydeclydebank answered 15/9, 2013 at 21:59 Comment(5)
Note that this isn't always VoiceOver friendly ("ADD ITEM" would read as "A.D.D. item") so maybe it's worth setting accessibilityLabel to the non-uppercase version.Corabella
@Blixt: Why would VoiceOver do per-letter on "add" but not "item"? I didn't get to reproduce this.Radiocarbon
@Radiocarbon I'm not sure exactly what the logic is, but "ADD" is a valid common acronym (Attention-Deficit Disorder), while ITEM is not, so that might be why. Otherwise, a more generic heuristic would be to speak out 1-3 uppercase letters as acronym to catch most common cases (BMW, ID, FAQ, AR, VR, SAT etc etc), but leave longer words alone as they may just be an upper case word.Corabella
Interesting! Thanks for the details.Radiocarbon
why is this an accepted answer? it makes string uppercased, not forcing label text to uppercase. you need to call text.uppercased() each time u set. textPinnatiped
C
22

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.

Chance answered 17/11, 2014 at 9:39 Comment(0)
P
21

This is the Swift 3 & Swift 4 version:

titleLabel.text = titleLabel.text?.uppercased()
Photomural answered 26/9, 2016 at 22:36 Comment(0)
M
8

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 
Mcginty answered 9/1, 2016 at 21:26 Comment(0)
T
4

A quick look into the documentation would have been quicker.

NSString *upperCase = [string uppercaseString];
Transmissible answered 15/9, 2013 at 21:59 Comment(0)
F
1

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)
  }
}
Forequarter answered 22/4, 2022 at 11:56 Comment(1)
this making a different size of letters than just making all characters uppercased. You need to increase the size of UIFont witch also increase the heigh of UILabel.Eugenle

© 2022 - 2024 — McMap. All rights reserved.