UIButton / 属性文本 - 可变行距

UIButton / Attributed Text - Varying line Spacing

提问人:Rufus 提问时间:10/18/2023 最后编辑:HangarRashRufus 更新时间:10/19/2023 访问量:24

问:

我在 Swift 5 项目中有一个继承自 UIButton 的组件。该按钮有两部分文本:标题(1 行)和副标题(1+ 行)。文本的两个部分被格式化为单个属性字符串,该字符串用作 的值。UIButton.titlelabel

我需要标题标题和副标题之间的行距是一个值,并且副标题每行之间的间距是一个不同的值。我尝试了以下方法,但无济于事(整个属性文本的间距设置为副标题间距)。是否可以有不同的线高,或者我需要不同的方法?ab

let buttonText = NSMutableAttributedString()

let titleStyle = NSMutableParagraphStyle()
titleStyle.lineHeightMultiple = 1
        
let title = NSAttributedString(string: title,
                               attributes: [
                               NSAttributedString.Key.font: UIFont.title3,
                               NSAttributedString.Key.paragraphStyle: titleStyle
                               ])
buttonText.append(title)

let subtitleStyle = NSMutableParagraphStyle()
subtitleStyle.lineHeightMultiple = 0.7
            
let subtitle = NSAttributedString(string: subtitle,
                                  attributes: [
                                  NSAttributedString.Key.font: UIFont.body,
                                  NSAttributedString.Key.paragraphStyle: subtitleStyle
                                ])
            
buttonText.append(subtitle)
iOS Swift UIBuint NSAttentdString

评论

0赞 DonMag 10/19/2023
如果您使用标签和设置,这会给您“不同的行距”吗?如果没有,那么它将在 .如果是,那么我不确定我们如何帮助您处理“组件......继承自 UIButton“,而没有看到代码。.attributedTextUIButton
0赞 DonMag 10/19/2023
这是你想要的吗?i.stack.imgur.com/iV2eD.png

答: 暂无答案