如何获得UIFont的lineSpacing? (以计算百分比值)

时间:2019-01-14 15:30:19

标签: ios uikit foundation uifont

是否可以将lineSpacing对象的NSParagraphStyle / NSMutableParagraphStyle属性的NSAttributedString属性设置为字体的基本/原始lineSpacing的百分比?

lineSpacingNSParagraphStyle的属性,而不是UIFont的属性,因此无法知道字体的原始lineSpacing是什么。

作为参考,这是我写的一个小扩展,用于将lineSpacing分配给UILabel(累加,不会导致标签的attributedString丢失信息):

import UIKit

extension UILabel {
    /// -important: must be called after setting the `text` property so the computed NSRange would be valid
    func setLineSpacing(with lineSpacing: CGFloat) {
        // get safe string
        guard let textString = self.text, !textString.isEmpty
            else { return }

        // get the range
        let entireRange: NSRange = (textString as NSString).range(of: textString)
        guard entireRange.isValidNSRange(within: textString)
            else { assertionFailure() ; return }

        // NSMutableAttributedText
        guard let mutableAttributedText: NSMutableAttributedString = self.attributedText?.mutableCopy() as? NSMutableAttributedString
            else { assertionFailure() ; return }

        // NSParagraphStyle
        var paragraphStyle: NSParagraphStyle? = mutableAttributedText.attribute(NSParagraphStyleAttributeName, at: 0, longestEffectiveRange: nil, in: entireRange) as? NSParagraphStyle
        if paragraphStyle == nil {
            // why TTTAttributedLabel :(
            paragraphStyle = NSParagraphStyle()
        }

        // safe NSParagraphStyle
        guard let safeParagraphStyle: NSParagraphStyle = paragraphStyle
            else { assertionFailure() ; return }

        // NSMutableParagraphStyle
        guard let mutableParagraphStyle: NSMutableParagraphStyle = safeParagraphStyle.mutableCopy() as? NSMutableParagraphStyle
            else { assertionFailure() ; return }

        // this is where the magic happens
        mutableParagraphStyle.lineSpacing = lineSpacing
        mutableAttributedText.addAttribute(NSParagraphStyleAttributeName, value: mutableParagraphStyle, range: entireRange)

        // assign attributed text which has all the existing attributes plus the new line spacing attribute which is inside the paragraphstyle attribute...
        self.attributedText = mutableAttributedText
    }
}

extension NSRange {
    // Is it safe to use range on the string?
    func isValidNSRange(within string: String) -> Bool {
        if self.location != NSNotFound
            && self.length > 0
            && self.location + self.length - 1 < string.length
            && self.location >= 0
        {
            return true
        } else {
            return false
        }
    }
}

我已经研究了问题,但在Stackoverflow上找不到答案。所以我要分享自己的FWIW。 (我想添加CoreText标记,以便可以轻松搜索,但我的声誉不允许我使用

1 个答案:

答案 0 :(得分:0)

解决方案

extension UIFont {
    func getLineSpacing(with percentage: CGFloat = 1) -> CGFloat {
        let lineSpacing: CGFloat = (self.lineHeight - self.pointSize)// /2

        return lineSpacing * percentage
    }
}

我基于我的解决方案this articlethis interesting Chinese article。更具体地说,我将附上我用来推断答案的部分的屏幕截图:

https://belka.us/en/uilabel-line-height-in-swift/

https://www.jianshu.com/p/50b3d434cbc0