gpt4 book ai didi

ios - 为 UITableView 滑动操作设置自定义字体 (UIContextualAction)

转载 作者:行者123 更新时间:2023-12-01 17:24:25 32 4
gpt4 key购买 nike

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

我已经尝试过 UIAppearance 但没有任何运气......

干杯! :)

最佳答案

我找到了一种使用图像属性而不是标题的方法...

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

Before/standard font

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

After/custom font

要创建标签图像,我有这个扩展:

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

关于ios - 为 UITableView 滑动操作设置自定义字体 (UIContextualAction),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56588715/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com