我正在 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
最佳答案
用于包装和截断标签文本的技术是根据您的用例将 lineBreakMode 属性值设置为 NSLineBreakByWordWrapping 或 NSLineBreakByTruncatingTail。当您将 UILabel 的 numberOfLines 属性设置为 0 时,这应该对您有用。
关于ios - 使用 NSMutableAttributedString 后,UILabel 中的 numberOfLines 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58691416/