UILabel带有两种不同颜色的文本

6

如何在UILabel中使用两种不同的字体颜色?我有两个不同的字符串,想要第一个字符串的文本为红色,第二个字符串的文本为绿色。两个字符串的长度均可变。


我在一个项目中也不得不这样做。我使用了另一篇帖子中的这个类,但它只适用于单行文本,我不知道对你是否足够。iPhone - UILabel containing text with multiple fonts at the same time - William Remacle
5个回答

8
尝试使用TTTAttributedLabel。它是UILabel的子类,支持NSAttributedString,这使得在同一字符串中具有多种颜色、字体和样式变得容易。
编辑:或者,如果您不想使用第三方依赖项并且目标是iOS 6,则UILabel现在具有attributedText属性。

1
不错。NSAttributedString 绝对是正确的选择 - 而你建议的这个类看起来非常不错。 - iceydee

7
您不能在UILabel中实现这一点。但我的建议是,不要使用多个UILabel,而是集中使用NSAttributedString。查找绘制NSAttributedStringUIControllers,因为UILabelUITextView不支持NSAttributedString
附注:如果您计划分发iOS6或更高版本的应用程序,由于UILabel现在支持NSAttributedString,您应该直接使用UILabel,而不是OHAttributedLabel,因为它现在已被操作系统本地支持。

这个答案不再正确,因为例如自iOS 6起,UILabel支持NSAttributedString。 - brainray
@brainray,我已经在去年更新了答案。请查看。 - KingofBliss

4

UILabel只能有一种颜色。你需要一个更复杂的元素,或者 - 可能更容易 - 只需使用两个单独的标签。使用[yourLabel sizeToFit];并相应地放置它们。


好的,我会尝试探索uilabel的sizeToFit属性,谢谢回复。 - pankaj

1

Swift 4
(注意:在Swift 4中,属性字符串键的符号已更改)

这是一个为 NSMutableAttributedString 添加/设置文本颜色的扩展。

extension NSMutableAttributedString {

    func setColor(color: UIColor, forText stringValue: String) {
        let range: NSRange = self.mutableString.range(of: stringValue, options: .caseInsensitive)
        self.addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: range)
    }

}

现在,尝试使用上述扩展与UILabel并查看结果。
let label = UILabel()
label.frame = CGRect(x: 40, y: 100, width: 280, height: 200)
let red = "red"
let blue = "blue"
let green = "green"
let stringValue = "\(red)\n\(blue)\n&\n\(green)"
label.textColor = UIColor.lightGray
label.numberOfLines = 0
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: stringValue)
attributedString.setColor(color: UIColor.red, forText: red)   // or use direct value for text "red"
attributedString.setColor(color: UIColor.blue, forText: blue)   // or use direct value for text "blue"
attributedString.setColor(color: UIColor.green, forText: green)   // or use direct value for text "green"
label.font = UIFont.systemFont(ofSize: 26)
label.attributedText = attributedString
self.view.addSubview(label)

这是关于编程的Swift 3解决方案:


extension NSMutableAttributedString {
        func setColorForText(textToFind: String, withColor color: UIColor) {
         let range: NSRange = self.mutableString.range(of: textToFind, options: .caseInsensitive)
          if range != nil {
            self.addAttribute(NSForegroundColorAttributeName, value: color, range: range)
          }
        }

}


func multicolorTextLabel() {
        var string: NSMutableAttributedString = NSMutableAttributedString(string: "red\nblue\n&\ngreen")
        string.setColorForText(textToFind: "red", withColor: UIColor.red)
        string.setColorForText(textToFind: "blue", withColor: UIColor.blue)
        string.setColorForText(textToFind: "green", withColor: UIColor.green)
        labelObject.attributedText = string
    }

Result:

enter image description here


刚在iOS 11中尝试了一下,扩展程序出现了错误.. self.addAttributes([NSForegroundColorAttributeName : color], range: range) - August
@August - 请分享错误信息。还请告知您的项目所使用的 Swift 版本。您可能正在尝试使用 Swift 3 扩展,而您的项目是使用的 Swift 4。请分享与错误相关的信息,这样我才能帮助您。 - Krunal
是的,这是一个Swift 4项目,并使用了您分享的Swift 4扩展。这是一个语法错误,而不是编译/运行时错误。文档 » https://developer.apple.com/documentation/foundation/nsmutableattributedstring/1414304-addattributes - August
1
...对于那个含糊的评论,我很抱歉。我发布了它,但忘记确保它实际上可以工作。因此,编辑时间超过了5分钟的限制。感谢您的跟进! - August

0
在iOS 6中,UILabel有NSAttributedString属性。因此,请使用它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接