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)
Custom font (Remove/Rename)
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
Gkmatchmaker Invite Handler Deprecated
Check Whether Time Falls Between Two Time iOS
Stick UItableview Header View to Top When Creating Header in Storyboard
Convert Cmsamplebufferref to UIimage
How to Sort Core Data Array Based on Date Entered
Two Button Simultaneous Press Input
Duplicate Collectionview Cells After Reloaddata with Firebase Query
Rgba to Abgr: Inline Arm Neon Asm for iOS/Xcode
How to Include Openssl on an iOS Project in a Way That Works
How to Rotate a UIview Without The Black Bars
How to Lock Viewcontroller in Portrait Mode
iOS Getting My Location Regularly and Setting Button to Get My Location - Google Map Not Working
Spritekit Swift Node Count Issues
Added a Custom Framework, Now Swift Can't Unarchive Data