【问题标题】:Force NSAttributedString line height with multiple font sizes使用多种字体大小强制 NSAttributedString 行高
【发布时间】:2020-03-20 15:40:53
【问题描述】:

我正在尝试使用具有相同行高的不同字体大小来显示属性字符串,这是我迄今为止尝试过的:

let paraStyle = NSMutableParagraphStyle()
paraStyle.maximumLineHeight = 30

let username = NSMutableAttributedString(string: user.username, attributes: [
    NSAttributedString.Key.font: UIFont(name: "Oswald-Medium", size: 17) as Any,
    NSAttributedString.Key.foregroundColor: UIColor(named: "DarkColor") as Any,
    NSAttributedString.Key.paragraphStyle: paraStyle
])

let onlineColor = UIColor(named: user.online ? "OnlineColor" : "OfflineColor")
let online = NSMutableAttributedString(string: "• ", attributes: [
    NSAttributedString.Key.font: UIFont.systemFont(ofSize: 24) as Any,
    NSAttributedString.Key.foregroundColor: onlineColor as Any,
    NSAttributedString.Key.baselineOffset: -4
])

let text = NSMutableAttributedString(string: content, attributes: [
    NSAttributedString.Key.font: UIFont(name: "Oswald-Regular", size: 14) as Any,
    NSAttributedString.Key.foregroundColor: UIColor(named: "TextColor") as Any
])

username.append(online)
username.append(text)

结果如下所示,我希望第一行的高度与其他行相同。

我玩过lineSpacinglineHeightMultiple,但总是有差距,知道如何解决这个问题吗?

【问题讨论】:

    标签: ios swift nsattributedstring


    【解决方案1】:

    将此添加到您的在线和文本中(NSMutableAttributedString):

        NSAttributedString.Key.paragraphStyle: paraStyle
    

    【讨论】:

    • 结果还是一样。
    猜你喜欢
    • 1970-01-01
    • 2013-04-13
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 2019-02-19
    相关资源
    最近更新 更多