如何在tableview单元格swift中显示动态多标签?

时间:2018-01-10 06:22:21

标签: ios uitableview swift3 uilabel nsattributedstring

我正在尝试NSMutableAttributedString,但没有获得完美的分辨率,也为每个字符串加下划线。

现在显示如下:

enter image description here

但我想这样:

enter image description here

我在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
}

0 个答案:

没有答案