在我的UILabel中,我想放3个不同颜色的3个字符串。 每个字符串的一种特定颜色。
但我不想使用NSRange(计算字符数)。我会找到一个改变整个字符串颜色的解决方案,无论长度如何。
例如:假设firstString为蓝色,secondString为绿色,thirdString为红色。
var firstString: [String] = "hello how"
var secondString: [String] = "are you"
var thirdString: [String] = "buddy?"
myLabel.text = "\(firstString) \(secondString) \(thirdString)"
使用此OUTPUT:
答案 0 :(得分:2)
这个,但是这是一个NSMutableAttributedString,你必须弄清楚如何使用这些,但是,无论你使用这个字符串,你最希望从超类头暴露一些东西暴露一个AttributedText您将为此属性赋值“myString1”的属性,然后您不会为“text”属性提供值。
var myString1 = NSMutableAttributedString(string:"hello how are you buddy")
let myString1Font1 = UIFont(name:"AvenirNext-Regular", size:24.0)
let myString1Color1 = UIColor(red: 0.292745, green: 0.461693, blue: 0.998524, alpha: 1.000000)
let myString1Color2 = UIColor(red: 0.000000, green: 0.176471, blue: 0.600000, alpha: 1.000000)
let myString1Color3 = UIColor(red: 0.333333, green: 0.556863, blue: 0.156863, alpha: 1.000000)
let myString1Color4 = UIColor(red: 0.643137, green: 0.031373, blue: 0.000000, alpha: 1.000000)
var myString1ParaStyle1 = NSMutableParagraphStyle()
myString1ParaStyle1.alignment = NSTextAlignment.Center
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:NSMakeRange(0,9))
myString1.addAttribute(NSUnderlineColorAttributeName, value:myString1Color1, range:NSMakeRange(0,9))
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:NSMakeRange(0,9))
myString1.addAttribute(NSForegroundColorAttributeName, value:myString1Color2, range:NSMakeRange(0,9))
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:NSMakeRange(9,8))
myString1.addAttribute(NSUnderlineColorAttributeName, value:myString1Color1, range:NSMakeRange(9,8))
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:NSMakeRange(9,8))
myString1.addAttribute(NSForegroundColorAttributeName, value:myString1Color3, range:NSMakeRange(9,8))
myString1.addAttribute(NSUnderlineColorAttributeName, value:myString1Color1, range:NSMakeRange(17,1))
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:NSMakeRange(17,1))
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:NSMakeRange(17,1))
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:NSMakeRange(18,5))
myString1.addAttribute(NSUnderlineColorAttributeName, value:myString1Color1, range:NSMakeRange(18,5))
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:NSMakeRange(18,5))
myString1.addAttribute(NSForegroundColorAttributeName, value:myString1Color4, range:NSMakeRange(18,5))
更好的答案,没有我添加到我之前的答案和自然对齐的下划线废话:
var myString1 = NSMutableAttributedString(string:"hello how are you buddy")
let myString1Font1 = UIFont(name:"Helvetica", size:14.0)
let myString1Color1 = UIColor(red: 0.000000, green: 0.176471, blue: 0.600000, alpha: 1.000000)
let myString1Color2 = UIColor(red: 0.247059, green: 0.411765, blue: 0.117647, alpha: 1.000000)
let myString1Color3 = UIColor(red: 0.607843, green: 0.172549, blue: 0.003922, alpha: 1.000000)
var myString1ParaStyle1 = NSMutableParagraphStyle()
myString1ParaStyle1.alignment = NSTextAlignment.Natural
myString1.addAttribute(NSForegroundColorAttributeName, value:myString1Color1, range:NSMakeRange(0,9))
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:NSMakeRange(0,9))
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:NSMakeRange(0,9))
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:NSMakeRange(9,1))
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:NSMakeRange(9,1))
myString1.addAttribute(NSForegroundColorAttributeName, value:myString1Color2, range:NSMakeRange(10,7))
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:NSMakeRange(10,7))
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:NSMakeRange(10,7))
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:NSMakeRange(17,1))
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:NSMakeRange(17,1))
myString1.addAttribute(NSForegroundColorAttributeName, value:myString1Color3, range:NSMakeRange(18,5))
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:NSMakeRange(18,5))
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:NSMakeRange(18,5))
哦,是的,我看到你想在UILabel中使用它,UILabel有一个名为“AttributedText”的属性,你应该把这个属性设置为我为你做的这个字符串不要设置“text”属性。
这样做
myLabel.attributedText = myString1
此外,这是简化的颜色:
let myString1Color1 = UIColor.blueColor()
let myString1Color2 = UIColor.greenColor()
let myString1Color3 = UIColor.redColor()
甚至是另一种方法:
var myString1 = NSMutableAttributedString(string:"hello how are you buddy?")
let myString1Font1 = UIFont(name:"Helvetica", size:14.0)
let myString1Color1 = UIColor.blueColor()
let myString1Color2 = UIColor.greenColor()
let myString1Color3 = UIColor.redColor()
let originalNSString = myString1.string as NSString
let myString1Range1 = originalNSString.rangeOfString("hello how ")
let myString1Range2 = originalNSString.rangeOfString("are you")
let myString1Range3 = originalNSString.rangeOfString(" ")
let myString1Range4 = originalNSString.rangeOfString("buddy?")
var myString1ParaStyle1 = NSMutableParagraphStyle()
myString1ParaStyle1.alignment = NSTextAlignment.Natural
myString1ParaStyle1.baseWritingDirection = NSWritingDirection.Natural
myString1ParaStyle1.defaultTabInterval = 0
myString1ParaStyle1.firstLineHeadIndent = 0
myString1ParaStyle1.headIndent = 0
myString1ParaStyle1.hyphenationFactor = 0
myString1ParaStyle1.lineBreakMode = NSLineBreakMode.ByWordWrapping
myString1ParaStyle1.lineHeightMultiple = 0
myString1ParaStyle1.lineSpacing = 0
myString1ParaStyle1.maximumLineHeight = 0
myString1ParaStyle1.minimumLineHeight = 0
myString1ParaStyle1.paragraphSpacing = 0
myString1ParaStyle1.paragraphSpacingBefore = 0
myString1ParaStyle1.tailIndent = 0
myString1.addAttribute(NSForegroundColorAttributeName, value:myString1Color1, range:myString1Range1)
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:myString1Range1)
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:myString1Range1)
myString1.addAttribute(NSForegroundColorAttributeName, value:myString1Color2, range:myString1Range2)
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:myString1Range2)
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:myString1Range2)
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:myString1Range3)
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:myString1Range3)
myString1.addAttribute(NSForegroundColorAttributeName, value:myString1Color3, range:myString1Range4)
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:myString1Range4)
myString1.addAttribute(NSFontAttributeName, value:myString1Font1!, range:myString1Range4)