简体   繁体   English

如何在 Swift 中使用属性字符串附加属性文本字符串

[英]how to append Attributed Text String with Attributed String in Swift

I want to append an Attributed Text with another Attributed Text in Swift.我想在 Swift 中附加一个属性文本和另一个属性文本。 Please provide any sample code for appending operation of two attributed String in Swift.请提供任何示例代码,用于在 Swift 中附加两个属性字符串的操作。

Use NSMutableAttributedString使用 NSMutableAttributedString

Small example小例子

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斯威夫特 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 represents your final string which contains both formattings provided by yourAttributes and yourOtherAttributes combination表示您的最终字符串,其中包含yourAttributesyourOtherAttributes提供的格式

@glace's answer , modified to avoid empty NSMutableAttributedString declaration. @glace 的回答,已修改以避免空的NSMutableAttributedString声明。 Valid in Swift 3.1:在 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: "This is an example ", attributes: yourAttributes)
let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: yourOtherAttributes)

partOne.append(partTwo)

partOne is then your final string with all the attributes. partOne是您的最终字符串,具有所有属性。 No intermediate "combiner" necessary.不需要中间的“组合器”。

Swift 4斯威夫特 4

let yourAttributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.black, .font: UIFont.systemFont(ofSize: 15)]
let yourOtherAttributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.red, .font: 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)

partOne.append(partTwo)

Swift 5斯威夫特 5

As per "glace" answer, I just update font attribute and swift version.根据“glace”的回答,我只更新字体属性和 swift 版本。

    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

using extension,使用扩展名,

extension NSMutableAttributedString{
    func getAttributedStringByAppending(attributedString:NSMutableAttributedString) -> NSMutableAttributedString{
        let newAttributedString = NSMutableAttributedString()
        newAttributedString.append(self)
        newAttributedString.append(attributedString)
        return newAttributedString
    }
}

Usage: attributedString1, attributedString2 are two NSMutableAttributedString, then用法:attributedString1,attributedString2 是两个 NSMutableAttributedString,然后

let combinedAttributedString = attributedString1.getAttributedStringByAppending(attributedString: attributedString2)

Swift 5斯威夫特 5

You can create the different attributes:您可以创建不同的属性:

let kString1Attributes = NSAttributedString.attributes(foregroundColor: UIColor.white)
let kString2Attributes = NSAttributedString.attributes(foregroundColor: UIColor.black)

Define your attributed strings:定义你的属性字符串:

let attrString1 = NSAttributedString(string: "my string" + " ",
                                     attributes: kString1Attributes)
let attrString2 = NSAttributedString(string: "string 2",
                                     attributes: kString2Attributes)

Include them in an array and combine them all together:将它们包含在一个数组中并将它们组合在一起:

let combinedAttrString = [attrString1, attrString2].joined()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM