我想要得到一個單一的UILabel文本進行修改,如下所示。如何在一個UILabel文本上獲取兩個普通字符串和NSMutableString?
怎樣才能得到它使用的NSMutableString或UILabel的attributedText財產做了什麼?幫助讚賞。
注意:名稱'John Doe'是從API派生的用戶帳戶名稱。
我想要得到一個單一的UILabel文本進行修改,如下所示。如何在一個UILabel文本上獲取兩個普通字符串和NSMutableString?
怎樣才能得到它使用的NSMutableString或UILabel的attributedText財產做了什麼?幫助讚賞。
注意:名稱'John Doe'是從API派生的用戶帳戶名稱。
請參考本例,添加合適的Attribute
。
let text = NSMutableAttributedString(string: "Welcome John Doe")
text.addAttribute(NSFontAttributeName, value: UIFont(name: "YourFont", size: 18)!, range: NSMakeRange(8, text.characters.count - 8))
label.attributedText = text
您可以使用屬性串:
let text = "Welcome John Doe"
let textWithColor = "John Doe"
let range = text.rangeOfString(textWithColor)
let attributedString = NSMutableAttributedString(string:text)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.orangeColor() , range: range)
label.attributedText = attributedString
我已經修改Ronit答案,並在下面的共享。
var userNameString:String?
override func viewWillAppear(animated: Bool) {
self.makeAttributedUserNameTitleLabelText()
}
func makeAttributedUserNameTitleLabelText()
{
userNameString = "John Doe"
let text = "Welcome "+userNameString!
let range = NSMakeRange(8, text.characters.count - 8)
let attributedString = NSMutableAttributedString(string:text)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.orangeColor(), range: range)
attributedString.addAttribute(NSFontAttributeName, value: UIFont(name: "Roboto-Bold", size: 14)!, range: range)
self.userNameTitleLabel.attributedText = attributedString
}
info.name = "John Doe" //which come from api
let nameValue = "Welcome " + "\(info.name!)"
let bSerString = "\(info.name!)"
let nameAttribt = NSMutableAttributedString(string: nameValue)
let bSRange = nameValue.rangeOfString(bSerString as String)
nameAttribt.addAttribute(NSFontAttributeName, value: UIFont(name: "OpenSans-Semibold", size: 14)!, range: bSRange)
nameAttribt.addAttribute(NSForegroundColorAttributeName, value: UIColor.orangeColor(), range: bSRange)
self.lblName.attributedText = nameAttribt