首页 > 解决方案 > 在 UIFont 上设置行高

问题描述

我了解您可以在 UI 标签和 UItextview 上设置行高。但是,我的问题是如何设置 UIfont 的行高?这可能吗?

做这样的事情是行不通的,因为 line-height 是一个 get only 属性,它似乎不是UIFont的一个minimum line Height或属性maximum line Height

    let font = UIFont.systemFont(ofSize: 12, weight: .bold)
    font.lineHeight = 12

标签: iosswift

解决方案


You can see the value of lineheight of your font via this extension :

CGFloat GetLineHeightForFont(CTFontRef iFont)
{
    CGFloat lineHeight = 0.0;
 
    check(iFont != NULL);
 
    // Get the ascent from the font, already scaled for the font's size
    lineHeight += CTFontGetAscent(iFont);
 
    // Get the descent from the font, already scaled for the font's size
    lineHeight += CTFontGetDescent(iFont);
 
    // Get the leading from the font, already scaled for the font's size
    lineHeight += CTFontGetLeading(iFont);
 
    return lineHeight;
}

推荐阅读