【发布时间】:2018-02-05 08:11:51
【问题描述】:
我想像这样输出
我的名字是 Pramod 塔帕尼亚
从下面的 json.
{
"text": "My name is B0$ Tapaniya.",
"formats": {
"formatId": [
"B0$"
],
"formatValue": [
"Pramod"
]
}
}
我尝试了下面的代码,但是因为我用属性字符串替换了字符串,所以出现了编译时错误。我使用 SwiftyJson 处理 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
【问题讨论】:
标签: ios json string swift3 nsattributedstring