Ignore Ascender and Descender when centering UILabel vertically?

前端 未结 3 1427
天命终不由人
天命终不由人 2021-01-12 17:08

I’m using AutoLayout to position some labels in the vertical centre of a cell. The text is in all-caps, but the UILabel in question, even when sizeToFit

3条回答
  •  醉酒成梦
    2021-01-12 18:08

    You can use the 'capHeight' and 'xHeight' properties on UIFont to get the correct height and use that to size the UILabel.

    Of course this assumes that you know for sure if a string would be lowercase or uppercase only. If not, then you can override setText on a UILabel and check every time the function gets called.

    I would also think of looking deeper into CoreText and implementing something like this http://www.zsiegel.com/2012/10/23/Core-Text-Calculating-line-heights/

提交回复
热议问题