我正在尝试NSMutableAttributedString
,但没有获得完美的分辨率,也为每个字符串加下划线。
现在显示如下:
但我想这样:
我在tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
中尝试使用此代码来显示单元格:
let arrSearchTag = search_detail?.value(forKey: "search_tags") as! NSArray
if arrSearchTag.count > 0
{
let str : NSMutableAttributedString = NSMutableAttributedString(string: "")
for string in arrSearchTag
{
let myString = string
let myString1 = " "
let myAttribute = [NSBackgroundColorAttributeName : UIColor.init(red: 94.0/255.0, green: 94.0/255.0, blue: 94.0/255.0, alpha: 1.0)]
let myAttribute1 = [NSBackgroundColorAttributeName : UIColor.clear]
let range = (myString as! NSString).range(of: myString as! String)
let myAttrString = NSMutableAttributedString(string: myString as! String, attributes: myAttribute)
let myAttrString1 = NSMutableAttributedString(string: myString1 , attributes: myAttribute1)
myAttrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.white, range: range)
str.append(myAttrString)
str.append(myAttrString1)
}
cell.lblSearchTags.attributedText = str
}