web-dev-qa-db-fra.com

comment ajouter une chaîne de texte attribuée avec une chaîne attribuée Swift

Je souhaite ajouter un texte attribué à un autre texte attribué dans Swift. Veuillez fournir un exemple de code pour l'ajout de l'opération de deux chaînes attribuées dans Swift.

43
jaydev

Utilisez NSMutableAttributedString

Petit exemple

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 représente votre chaîne finale qui contient les deux mises en forme fournies par yourAttributes et yourOtherAttributes

114
glace

réponse de @ glace , modifié pour éviter la déclaration vide NSMutableAttributedString. Valable dans 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 est alors votre dernière chaîne avec tous les attributs. Aucun "combineur" intermédiaire nécessaire.

Swift 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)
14
leanne

Swift 5

Selon la réponse "glace", je viens de mettre à jour les attributs de police et Swift version.

    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
1
Vivek

en utilisant l'extension,

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

Utilisation: attribuéString1, attribuéString2 sont deux NSMutableAttributedString, puis

let combinedAttributedString = attributedString1.getAttributedStringByAppending(attributedString: attributedString2)
0
cnu