Search code examples
uitableviewswiftuicolornsmutableattributedstring

How to create attributed text in a UITableView?


So I have a class:

class BoatTypeGame: UITableViewController

The table view has 14 rows. From rows 1-7, I want to make the color of the text green and for rows 8-14, I want to make the color of the text red. By the way, the cells will not have the same text every time the view loads; I used the arc4random_uniform() method to randomize the text. Thank you!


Solution

  • In your cellForRowAtIndexPath or even tableViewWillDisplayCell just use a switch statement or an if statement.

    switch indexPath.row {
    case 1...7:
        println("row 1-7")
        cell.textLabel?.textColor = UIColor.greenColor()
    case 8...14:
        println("row 8-14")
        cell.textLabel?.textColor = UIColor.redColor()
    default:
        break
    }