1

How do I change the textColor of a label or a cell on selection in iOS Swift?

I want the background to not to change. Only textColor and seperatorColor if any. Something like:

label.highlightedTextColor = UIColor.whiteColor();

I have seen this happen in some apps that the color changes. But I cannot get any near to it. According to Apple Dev Reference:

Subclasses that use labels to implement a type of text button can use the value in this property when drawing the pressed state for the button. This color is applied to the label automatically whenever the highlighted property is set to true.

But, labels compile fine and don't change color on highlight. Buttons do not have highlightedTextColor

user3581203
  • 297
  • 1
  • 2
  • 13
  • So, what do you have so far? – Sebastian Jan 14 '15 at 08:54
  • @SebastianDressler I did not find any methods in reference to Dev Docs. – user3581203 Jan 14 '15 at 08:55
  • `UILabel` does not support selection, you may want to use `UITextField`. For `UITableCell` there is e.g. [UITableView Cell selected Color?](http://stackoverflow.com/questions/1998775/uitableview-cell-selected-color) – Sebastian Jan 14 '15 at 08:57
  • @SebastianDressler I mentioned `NOT BACKGROUND COLOR`. And I specially need it for `UITableViewCell`. – user3581203 Jan 14 '15 at 08:59
  • 2
    Alright, then how about [UITableView cell textLabel color](http://stackoverflow.com/questions/9733267/uitableview-cell-textlabel-color). It even shows the approach when to set it. It's Objective-C but easily adaptable IMO. – Sebastian Jan 14 '15 at 09:01
  • 1
    @SebastianDressler Does not work in Swift – tika Jan 14 '15 at 09:07
  • 1
    What exactly does not work in Swift? – Sebastian Jan 14 '15 at 09:08
  • @SebastianDressler the Code above. It complies fine. Does not give a highlight – user3581203 Jan 14 '15 at 09:26
  • Sorry, but I don't get it. The code I linked to is Objective-C code, obviously you have to adapt it to your project. The answer by @rakeshbs provides the exact same information but in Swift. I.e. it is not clear, why his code works and the other (after being adopted to Swift) should not work or is not working. – Sebastian Jan 14 '15 at 09:28

5 Answers5

8

I've tried setting it in didSelectAtIndexPath and didDeselectAtIndexPath but the color was getting changed with a delay. I didn't like that.

This is how I ended up solving the problem:

I have subclassed my UITableViewCell and connected all the @IBOutlets. In awakeFromNib() I simply did this:

@IBOutlet weak var myLabel: UILabel!


override func awakeFromNib() {
    super.awakeFromNib()        

    self.myLabel.highlightedTextColor = .blackColor()

}

Works perfectly now!

btw this is my first ever answer on stack overflow please be nice to me hehe

(I'm using Swift 2.0 and Xcode 7.2.1)

Mario Subotic
  • 131
  • 1
  • 5
7

if you (use/not use) a custom cell(as xib), you can select the lable, and from utilities, go to highlighted and change the color to the color you want to show in selection. and this is a snapshot to be more clear :) snapshot

Rawan
  • 1,589
  • 4
  • 23
  • 47
5

You can set the color in didSelectAtIndexPath and didDeselectAtIndexPath and cellForRowAtIndexPath

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.cellForRowAtIndexPath(indexPath)?.textLabel?.textColor = UIColor.blackColor()
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.cellForRowAtIndexPath(indexPath)?.textLabel?.textColor = UIColor.redColor()
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    // Dequeue cell
    cell.textLabel?.textColor = UIColor.redColor()
}
rakeshbs
  • 24,392
  • 7
  • 73
  • 63
  • plus one for the logic but since I am using navigation bars, the text still remains highlighted when I come back. And also, the performance is very poor. Only highlights when segue starts. – user3581203 Jan 14 '15 at 09:24
  • what do you mean only highlights when segue starts? – rakeshbs Jan 14 '15 at 09:41
  • @rakeshbs I think there are some other problems and misunderstandings which we are not aware of. – Sebastian Jan 14 '15 at 09:49
1

In the cell itself in the method setSelected you can override it and then provide whatever colour or highlighted state you want:

override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated)

    if isSelected {
        // your color or highlited here
    } else {
       // your colour or highlighted here
    }
}
0

In cellForRowAt indexPath you can just add this line

cell.textLabel?.textColor = UIColor.blackColor()
zx485
  • 28,498
  • 28
  • 50
  • 59