首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获得lineSpacing of UIFont的百分比?(计算百分比值)

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

Stack Overflow用户
提问于 2019-01-14 15:30:18
回答 1查看 185关注 0票数 1

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

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

作为参考,下面是我为将lineSpacing分配给UILabel而编写的一个小小的扩展(累积的,不会导致标签的attributedString上的信息丢失):

代码语言:javascript
复制
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标签,这样就可以很容易地搜索到,但我的名声不允许我)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-14 15:38:21

解决方案

代码语言:javascript
复制
extension UIFont {
    func getLineSpacing(with percentage: CGFloat = 1) -> CGFloat {
        let lineSpacing: CGFloat = (self.lineHeight - self.pointSize)// /2

        return lineSpacing * percentage
    }
}

我基于我的解决方案这篇文章这篇有趣的中文文章。更具体地说,我附上了我用来推断答案的部分的截图:

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54184432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档