具有多个字体大小的强制NSAttributedString行高

问题描述 投票:0回答:1

我正在尝试使用具有相同行高的不同字体大小显示属性字符串,这是到目前为止我已经尝试过的内容:

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)

结果看起来像这样,我希望第一行具有与其他行相同的高度。

enter image description here

我玩过lineSpacinglineHeightMultiple,但始终存在差距,关于如何解决这个问题的任何想法?

ios swift nsattributedstring
1个回答
0
投票

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

    NSAttributedString.Key.paragraphStyle: paraStyle
© www.soinside.com 2019 - 2024. All rights reserved.