我想在Swift中添加一个属性文本和另一个属性文本。请提供任何示例代码,用于在Swift中附加两个属性String的操作。
答案 0 :(得分:110)
使用NSMutableAttributedString
小例子
let yourAttributes = [NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont.systemFontOfSize(15)]
let yourOtherAttributes = [NSForegroundColorAttributeName: UIColor.redColor(), NSFontAttributeName: UIFont.systemFontOfSize(25)]
let partOne = NSMutableAttributedString(string: "This is an example ", attributes: yourAttributes)
let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: yourOtherAttributes)
let combination = NSMutableAttributedString()
combination.appendAttributedString(partOne)
combination.appendAttributedString(partTwo)
Swift 3
let yourAttributes = [NSForegroundColorAttributeName: UIColor.black, NSFontAttributeName: UIFont.systemFont(ofSize: 15)]
let yourOtherAttributes = [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: UIFont.systemFont(ofSize: 25)]
let partOne = NSMutableAttributedString(string: "This is an example ", attributes: yourAttributes)
let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: yourOtherAttributes)
let combination = NSMutableAttributedString()
combination.append(partOne)
combination.append(partTwo)
combination
代表您的最终字符串,其中包含yourAttributes
和yourOtherAttributes
答案 1 :(得分:14)
答案 2 :(得分:0)
使用扩展名
extension NSMutableAttributedString{
func getAttributedStringByAppending(attributedString:NSMutableAttributedString) -> NSMutableAttributedString{
let newAttributedString = NSMutableAttributedString()
newAttributedString.append(self)
newAttributedString.append(attributedString)
return newAttributedString
}
}
用法: attributedString1,attributedString2是两个NSMutableAttributedString,然后
let combinedAttributedString = attributedString1.getAttributedStringByAppending(attributedString: attributedString2)
答案 3 :(得分:0)
根据“ glace”的答案,我只是更新字体属性和快速版本。
let boldFontAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 17)]
let normalFontAttributes = [NSAttributedString.Key.foregroundColor: UIColor.darkGray, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15)]
let partOne = NSMutableAttributedString(string: "This is an example ", attributes: boldFontAttributes)
let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: normalFontAttributes)
let combination = NSMutableAttributedString()
combination.append(partOne)
combination.append(partTwo)
lblUserName.attributedText = combination
答案 4 :(得分:0)
您可以创建不同的属性:
let kString1Attributes = NSAttributedString.attributes(foregroundColor: UIColor.white)
let kString2Attributes = NSAttributedString.attributes(foregroundColor: UIColor.black)
定义你的属性字符串:
let attrString1 = NSAttributedString(string: "my string" + " ",
attributes: kString1Attributes)
let attrString2 = NSAttributedString(string: "string 2",
attributes: kString2Attributes)
将它们包含在一个数组中并将它们组合在一起:
let combinedAttrString = [attrString1, attrString2].joined()