如何使用Swift中的归属字符串追加属性文本字符串

时间:2016-04-07 06:10:21

标签: swift nsattributedstring

我想在Swift中添加一个属性文本和另一个属性文本。请提供任何示例代码,用于在Swift中附加两个属性String的操作。

5 个答案:

答案 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代表您的最终字符串,其中包含yourAttributesyourOtherAttributes

提供的格式

答案 1 :(得分:14)

@glace的回答,修改为避免空NSMutableAttributedString声明。在Swift 3.1中有效: let yourAttributes = [NSForegroundColorAttributeName:UIColor.blackColor(),NSFontAttributeName:UIFont.systemFontOfSize(15)] let yourOtherAttributes = [NSForegroundColorAttributeName:UIColor.redColor(),NSFontAttributeName:UIFont.systemFontOfSize(25)] let partOne = NSMutableAttributedString(string:"这是一个例子",属性:yourAttributes) 让partTwo = NSMutableAttributedString(字符串:"对于归因字符串的组合!",属性:yourOtherAttributes) partOne.append(partTwo) partOne是具有所有属性的最终字符串。没有中间体"组合器"必要。 斯威夫特4 let yourAttributes:[NSAttributedString.Key:Any] = [.foregroundColor:UIColor.black,.font:UIFont.systemFont(ofSize:15)] 让yourOtherAttributes:[NSAttributedString.Key:Any] = [.foregroundColor:UIColor.red,.font:UIFont.systemFont(ofSize:25)] let partOne = NSMutableAttributedString(string:"这是一个例子",属性:yourAttributes) 让partTwo = NSMutableAttributedString(字符串:"对于归因字符串的组合!",属性:yourOtherAttributes) partOne.append(partTwo)

答案 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)

雨燕5

根据“ 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)

斯威夫特 5

您可以创建不同的属性:

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()