ios - 使用 NSMutableAttributedString 后,UILabel 中的 numberOfLines 不工作

标签 ios swift string colors uilabel

我正在 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/

相关文章:

html - 在 swift 中解析 html 的最佳实践是什么?

ios - 从外部 UIViewController 更新 SpriteKit 场景中的节点

python - 如何在 Python 中合并两个 json 字符串?

c# - 将 String[] 转换为 byte[] 数组

ios - 每次检测 WKWebView 何时完成加载

ios - didSelectRowAtIndexPath 不起作用?

swift - 用协变类型的属性满足协议(protocol)要求

regex - PostgreSQL 9 : substring with regex to get string between string and pipe

ios - 在 Swift 中打印数据的大小(兆字节)

android - Firebase 移动应用程序跟踪 - 使用哪些库?