【发布时间】:2019-11-04 10:23:05
【问题描述】:
我在 UILabel 中做多色文本。它运作良好。但问题是我明白了,我可以在 UIlabel 中显示多行。这意味着 numberOfLine 属性不起作用。
extension NSMutableAttributedString {
func setColorForText(textForAttribute: String, withColor color: UIColor) {
let range: NSRange = self.mutableString.range(of: textForAttribute, options: .caseInsensitive)
self.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
}
let stringValue = "Natural air is most important part of human body. people got too much problem because of it. please everybody try to do best part for it"
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: stringValue)
attributedString.setColorForText(textForAttribute: "\(item.catName ?? "")", withColor: Colors.green)
postInfoLabel.font = UIFont.systemFont(ofSize: ViewSize.width/33)
postInfoLabel.numberOfLines = 0
postInfoLabel.attributedText = attributedString
【问题讨论】:
-
numberOfLines = 0 表示任意行数。
-
所以你想在一个标签中显示多行文本但你只有一个?
-
我使用 numberOfLines = 0。字符串应该显示为两行或三行。但它只显示一行
-
bazyli7 是对的。你是对的朋友。 bazyl87
-
检查您是否对标签应用了适当的约束?
标签: ios swift string colors uilabel