【问题标题】:Multiline multicolor attributed text in swiftswift中的多行多色属性文本
【发布时间】:2018-09-15 15:49:34
【问题描述】:

如何使用属性文本构建这样的内容?我尝试添加换行符(\n),它不起作用,只显示第一行并裁剪下两个,删除 \n 将在单行中显示两个部分:

let mutableAttributedString = NSMutableAttributedString()

let regularAttribute = [NSAttributedStringKey.font: UIFont(name: "Avenir-Light", size: 45.0), NSAttributedStringKey.foregroundColor: UIColor.yellow]
let boldAttribute = [NSAttributedStringKey.font: UIFont(name: "Avenir-Light", size: 25.0), NSAttributedStringKey.foregroundColor: UIColor.white]     

let mutableAttributedString2 = NSMutableAttributedString()
let boldAttributedString = NSAttributedString(string: "person", attributes: boldAttribute)
let regularAttributedString = NSAttributedString(string: "\(self.requestCount)", attributes: regularAttribute)
mutableAttributedString2.append(boldAttributedString)
mutableAttributedString2.append(NSAttributedString(string: "\n"))
mutableAttributedString2.append(regularAttributedString)

self.btnStatsPeople.setAttributedTitle(mutableAttributedString2, for: .normal)

【问题讨论】:

标签: swift nsattributedstring


【解决方案1】:

UILabel 默认有一个line

此属性控制要使用的最大行数,以便 使标签的文本适合其边界矩形。默认值 对于这个属性是 1. 删除任何最大限​​制,并使用尽可能多的 行,根据需要将此属性的值设置为 0。

UIButton 创建默认的 UILabel。因此,使用 0 行可以解决您的问题:

self.btnStatsPeople.titleLabel?.numberOfLines = 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-29
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 2014-01-20
    相关资源
    最近更新 更多