简体   繁体   中英

UITableViewCell - UILabel with attributed text

I have a table view with cells that have a single label inside, nothing strange. I want that this single label contains a text with 2 different colors:

let red = NSAttributedString(string: "Red color", 
                         attributes:
                              [NSForegroundColorAttributeName: UIColor.redColor(),
                                          NSFontAttributeName: normalFont])
let blue = NSAttributedString(string: "Blue color", 
                          attributes: 
                              [NSForegroundColorAttributeName: UIColor.blueColor(),
                                          NSFontAttributeName: boldFont])

let attributedText = NSMutableAttributedString(attributedString: red)
attributedText.appendAttributedString(blue)

self.labelInformation.attributedText = attributedText

Problem: text is all blue and I can't understand why.

If I print out attributedText I get:

Red color
{
    NSColor = "UIExtendedSRGBColorSpace 1 0 0 1";
    NSFont = "<UICTFont: 0x104249d80> font-family: \"Open Sans\"; font-weight: normal; font-style: normal; font-size: 18.00pt";
}Blue color{
    NSColor = "UIExtendedSRGBColorSpace 0 0 1 1";
    NSFont = "<UICTFont: 0x10b51d7c0> font-family: \"Open Sans\"; font-weight: bold; font-style: normal; font-size: 20.00pt";
}

That seems ok!

Issue has to be related to table view since that same code applied on a view works.

Any advice?

Update

This code is inside a method of a custom cell view:

class CustomViewCell: UITableViewCell {

    func setup() {
       // code above
    }

}

In controller:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("CustomIdentifier") as! CustomTableViewCell
    cell.setup()
    return cell
}

Try Something Like That

myMutableString.addAttribute(NSFontAttributeName,
    //: Make a big blue P
        myMutableString.addAttribute(
            NSFontAttributeName,
            value: UIFont(
                name: "AmericanTypewriter-Bold",
                size: 36.0)!,
            range: NSRange(
                location:0,
                length:1))
        myMutableString.addAttribute(
            NSForegroundColorAttributeName,
            value: UIColor.blue(),
            range: NSRange(
                location:0,
                length:1))

Visit this link very well explained

I find the mistake in AppDelegate I have:

 UILabel.appearance().textColor = color

So the solution is to put this code on cell class:

override func awakeFromNib() {
    super.awakeFromNib()
    self.labelInformation.text = nil
    self.labelInformation.textColor = nil
}

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