gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:36:46 25 4
gpt4 key购买 nike

有没有办法设置 lineSpacing NSParagraphStyle 的属性(property)/ NSMutableParagraphStyle NSAttributedString 的属性反对字体的基本/原始的百分比lineSpacing ?
lineSpacingNSParagraphStyle 的属性而不是 UIFont所以不可能知道原来的lineSpacing对于字体是。

作为引用,这是我为分配 lineSpacing 编写的一个小扩展。到 UILabel (累积,不会导致标签的属性字符串信息丢失):

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 标签,以便轻松搜索,但我的声誉不允许我这样做)

最佳答案

解决方案

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

关于ios - 如何获得 UIFont 的百分比 lineSpacing? (计算百分比值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54184432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com