Swift How to calculate one line text height from its font

Ismail picture Ismail · Feb 29, 2016 · Viewed 14.7k times · Source

I ran into an issue where I needed to animate translating a label vertically the same distance of a textField's text height. In most cases just textField.bounds.heigt but if the textField's height is bigger than the text height it will not be any good for me. So I need to know: How to calculate the line height of the string text from its UIFont?

Regarding the duplicate: There's a little bit different of what I need. that answer(which I've referenced in my answer) get the total height depending on 1) the string 2) the width 3) the font. What I needed is one line height dpending only on the font.

Answer

Alexandr Kolesnik picture Alexandr Kolesnik · Aug 11, 2016

UIFont has a property lineHeight:

    if let font = _textView.font {
        let height = font.lineHeight
    }

where font is your font