xcode 带有两种不同颜色文本的 UILabel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3952571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UILabel with two different color text
提问by pankaj
How can i have a UILabel
with two different colors for the font? I will have text in two different strings and i want to make text with first string as redand second as green. The length of both the string is variable.
我怎样才能有UILabel
两种不同颜色的字体?我将有两个不同字符串中的文本,我想使文本的第一个字符串为红色,第二个为绿色。两个字符串的长度都是可变的。
回答by mattt
Try TTTAttributedLabel. It's a subclass of UILabel that supports NSAttributedString
s, which would make it easy to have multiple colors, fonts, and styles in the same string.
试试TTTAttributedLabel。它是支持NSAttributedString
s的 UILabel 的子类,这使得在同一个字符串中具有多种颜色、字体和样式变得容易。
Edit: Alternatively, if you don't want the 3rd party dependency and are targeting iOS 6, UILabel
now has the attributedText
property.
编辑:或者,如果您不想要第 3 方依赖项并且针对 iOS 6,UILabel
现在拥有该attributedText
属性。
回答by KingofBliss
You can't do this within a UILabel
s. But my suggestion is that instead of using multiple UILabel
just concentrate on NSAttributedString
. Find UIControllers
that draw NSAttributedString
because UILabel
, UITextView
do not support NSAttributedString
.
您不能在UILabel
s内执行此操作。但我的建议是,不要使用多个,UILabel
而是专注于NSAttributedString
. 找UIControllers
那个平局NSAttributedString
是因为UILabel
,UITextView
不支持NSAttributedString
。
PS: if you plan to distribute an iOS6 or later application, as UILabel now support NSAttributedString, you should use UILabel directly instead of OHAttributedLabel as it is now natively supported by the OS.
PS:如果你打算发布一个 iOS6 或更高版本的应用程序,因为 UILabel 现在支持 NSAttributedString,你应该直接使用 UILabel 而不是 OHAttributedLabel,因为它现在被操作系统原生支持。
回答by Eiko
UILabel can only have onecolor. You either need a more sophisticated element, or - probably easier - just use two separate labels. Use [yourLabel sizeToFit];
and place them accordingly.
UILabel 只能有一种颜色。您要么需要更复杂的元素,要么 - 可能更简单 - 只需使用两个单独的标签。相应地使用[yourLabel sizeToFit];
和放置它们。
回答by Krunal
Swift 4
(Note: notation for attributed string key is changed in swift 4)
Swift 4
(注意:属性字符串键的符号在 swift 4 中发生了变化)
Here is an extension for NSMutableAttributedString
, that add/set color on string/text.
这是 的扩展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)
}
}
Now, try above extension with UILabel
and see result
现在,尝试上面的扩展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)
Here is solution in Swift 3:
这是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:
结果:
回答by Mann
In iOS 6 UILabel has NSAttributedString property. So use that.
在 iOS 6 UILabel 有 NSAttributedString 属性。所以用那个。