简体   繁体   中英

Using attributed text programmatically in label inside cell

Currently I have the following text inside a label located in a cell:

var textvalue: String? = nil

        switch indexPath.row {
        case 0:
            textvalue = "Person: \(`pow`.name)"
            break
        case 2:
            textvalue = "Place: \(`pow`.address)"
            break
        default:
            fatalError("Error")
        }

    cell.textLabel?.text = textvalue

        return cell
    }

How can only the text Person: and Place: be bolded? Instead of the entire text string?

Do like below:

    var textvalue: String?

    switch indexPath.row {
    case 0:
       cell.textLabel?.attributedText =
       NSMutableAttributedString()
           .bold("Person: ")
           .normal(`pow`.name)
       break
    case 2:
       cell.textLabel?.attributedText =
       NSMutableAttributedString()
           .bold("Place: ")
           .normal(`pow`.address)
       break
    default:
       fatalError("Error")
    }

    return cell
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM