iosiphoneuitableviewswiftuitableviewrowaction

How to do custom font and color in UITableViewRowAction without Storyboard


I have classic TableView where you can delete item if you swipe and than clicking on the button. I know how to set custom background on the cell, but I can't find how I can set custom font and color for that.

Thank you for help!

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]?  {

    var deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, 
                   title: "Delete", 
                   handler: { 
                      (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
                           println("Delete button clicked!")
                   })

    deleteAction.backgroundColor = UIColor.redColor()

    return [deleteAction]
}

Solution

  • How to use a custom font?

    It's pretty easy.

    1. Firstly, you need to include your custom font files to your project.
    2. Next, go to your info.plist file and add a new entry with the key "Fonts provided by application". Note that this entry should be an Array.
    3. Then add the names of these files as elements of this array.

    And that's it! All you need then is to use the font by its name like this

    cell.textLabel.font = [UIFont fontWithName:@"FontName" size:16];

    How to change the font color?

    Even easier. All you need is

    cell.textlabel.textcolor = UIColor.redColor()

    Edit:

    In your case you want to change the font of the RowAction. So I think of only 2 solutions. One to use [UIColor colorWithPatterImage:]

    Or you can user [[UIButton appearance] setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; because the RowAction contains a button.