Je veux sortie commeRemplacer la chaîne attribuée en NSMutableAttributedString Dans Swift 3
Mon nom est Pramod Tapaniya
en bas JSON. J'ai essayé le code ci-dessous, mais obtenant l'erreur de temps de compilation parce que je remplace la chaîne avec la chaîne attribuée. J'ai utilisé SwiftyJson pour gérer json.
let boldAttributes = [NSForegroundColorAttributeName: UIColor.white, NSFontAttributeName: UIFont.boldSystemFont(ofSize: 13)]
let simpleAttributes = [NSForegroundColorAttributeName: UIColor.lightGray, NSFontAttributeName: UIFont.systemFont(ofSize: 13)]
let strComm = NSMutableAttributedString(string: dict["text"].stringValue, attributes: simpleAttributes)
let attributedStr = NSMutableAttributedString()
attributedStr.append(strComm)
let arrFormatId = dict["formats"]["formatId"]
let arrFormatValue = dict["formats"]["formatValue"]
for var i in 0..<arrFormatId.count{
let strBold = NSMutableAttributedString(string: arrFormatValue[i].stringValue, attributes: boldAttributes)
attributedStr.mutableString.replaceOccurrences(of: arrFormatId[i].stringValue, with: strBold, options: .caseInsensitive, range: NSRange(location: 0, length: attributedStr.length)) //Cannot convert value of type 'NSMutableAttributedString' to expected argument type 'String'
}
return attributedStr
"Pramod" en gras. Vous venez de remplacer la chaîne. –
Mise à jour de la réponse. –