4

My app is using custom font to display an information for the whole app.

But there is some problems with their base line like in the image below:

enter image description here

I need to fix "X 4" to be centered in vertical red box.

How can I fix this?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Varis Darasirikul
  • 3,013
  • 5
  • 25
  • 35

2 Answers2

4

Set attributed string with adjusted baseline offset like so in Swift:

let sttrStr = NSAttributedString.init(string: "your text",
                                      attributes:[NSAttributedStringKey.baselineOffset: -10])
textLabel.attributedText = attrStr

or like so in ObjC:

NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:@"your string"
                              attributes:@{ NSBaselineOffsetAttributeName : @-10 }];
textLabel.attributedText = attrStr;

Per SO answer here

Vlad
  • 5,727
  • 3
  • 38
  • 59
1

You can create a UILabel and set its size same as the red box. And then set it's text alignment as center:

youLabel.textAlignment = NSTextAlignment.Center

Another not so good option is to make a UIButton of same size as red box and set its UserInteractionEnabled to No. This way your text will be positioned in the center horizontally and vertically. You can also set it's setContentVerticalAlignment property

Munahil
  • 2,381
  • 1
  • 14
  • 24