设置UITableView滑动操作(UIContextualAction)的自定义字体 [英] Set custom font for UITableView swipe action (UIContextualAction)

查看:1835
本文介绍了设置UITableView滑动操作(UIContextualAction)的自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为UIContextualAction中的标题设置自定义字体?

How do you set a custom font for the title in UIContextualAction?

我尝试了UIAppearance,但是没有任何运气...

I have tried UIAppearance but without any luck...

干杯! :)

推荐答案

我已经找到一种方法,可以使用image属性而不是title ...

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

标准字体(删除/重命名)

自定义字体(删除/重命名)

要创建标签的图像,请使用以下扩展名:

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
        }
    }
}

然后我就拥有:

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])
}

这篇关于设置UITableView滑动操作(UIContextualAction)的自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆