Set Custom Font for UItableview Swipe Action (Uicontextualaction)

Set custom font for UITableView swipe action (UIContextualAction)

I have found a way to do this by using the image property instead of the title...

Standard font (Remove/Rename)

Before/standard font

Custom font (Remove/Rename)

After/custom font

To create an image of a label I have this extension:

extension UIImage {

/// This method creates an image of a view
convenience init?(view: UIView) {

// Based on https://stackoverflow.com/a/41288197/1118398
let renderer = UIGraphicsImageRenderer(bounds: view.bounds)
let image = renderer.image { rendererContext in
view.layer.render(in: rendererContext.cgContext)
}

if let cgImage = image.cgImage {
self.init(cgImage: cgImage, scale: UIScreen.main.scale, orientation: .up)
} else {
return nil
}
}
}

And then I simply have:

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

let action = UIContextualAction(style: .destructive, title: nil) { action, view, completion in
// Your swipe action code!
}
let label = UILabel()
label.text = // Your swipe action text!
label.font = // Your custom font!
label.sizeToFit()
action.image = UIImage(view: label)

return UISwipeActionsConfiguration(actions: [action])
}

How to change UITableViewRowAction title color?

I'm afraid that there's no way to change the title color of the UITableViewRowAction.

The only things you can change on the action are:

  • backgroundColor
  • style (destructive (red backgroundcolor, ...)
  • title

For more info, please refer to the Apple Doc UITableViewRowAction

UIContextualAction icon and text alignment

It doesn't appear there is an answer to why the text appears, and appears mis-aligned.

For anyone who comes across this question, we ended up setting the titles to nil for these buttons to keep them "icon only".



Related Topics



Leave a reply



Submit