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])
}
func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
if #available(iOS 13.0, *) {
for subview in tableView.subviews {
if NSStringFromClass(type(of: subview)) == "_UITableViewCellSwipeContainerView" {
for swipeContainerSubview in subview.subviews {
if NSStringFromClass(type(of: swipeContainerSubview)) == "UISwipeActionPullView" {
for case let button as UIButton in swipeContainerSubview.subviews {
button.titleLabel?.font = .systemFont(ofSize: 12)
}
}
}
}
}
} else {
for subview in tableView.subviews {
if NSStringFromClass(type(of: subview)) == "UISwipeActionPullView" {
for case let button as UIButton in subview.subviews {
button.titleLabel?.font = .systemFont(ofSize: 12)
}
}
}
}
}
extension UITableView {
/// Iterates over all subviews of a `UITableView` instance and applies the supplied font to all labels withing the UISwipeAction's array.
/// - Parameter font: The font that should be applied to the labels.
/// - Parameter tintColor: The tint color that should be applied to image views and labels
/// - Parameter ignoreFirst: Whether or not the first swipe action should be ignored when applying tints
public func setSwipeActionFont(_ font: UIFont, withTintColor tintColor: UIColor? = nil, andIgnoreFirst ignoreFirst: Bool = false) {
for subview in self.subviews {
//Confirm that the view being touched is within a swipe container
guard NSStringFromClass(type(of: subview)) == "_UITableViewCellSwipeContainerView" else {
continue
}
//Re-iterate subviews and confirm that we are touching a swipe view
for swipeContainerSubview in subview.subviews {
guard NSStringFromClass(type(of: swipeContainerSubview)) == "UISwipeActionPullView" else {
continue
}
//Enumerate subviews and confirm that we are touching a button
for (index, view) in swipeContainerSubview.subviews.filter({ $0 is UIButton }).enumerated() {
//Set Font
guard let button = view as? UIButton else {
continue
}
button.titleLabel?.font = font
//Set Tint Conditionally (based on index)
guard index > 0 || !ignoreFirst else {
continue
}
button.setTitleColor(tintColor, for: .normal)
button.imageView?.tintColor = tintColor
}
}
}
}
}
extension UIView {
// Using a function since `var image` might conflict with an existing variable
// (like on `UIImageView`)
func asImage() -> UIImage {
let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image { rendererContext in
layer.render(in: rendererContext.cgContext)
}
}
}
5条答案
按热度按时间mkshixfv1#
我已经找到了一种方法来做到这一点,通过使用图像属性,而不是标题。。
标准字体(删除/重命名)
自定义字体(删除/重命名)
要创建标签的图像,我有以下扩展名:
然后我简单地有:
8ftvxx2r2#
我最近发现了一种方法,通过使用button titleLabel代替image属性来实现这一点,这样您就可以保留对文本和图像进行操作的能力。
如你所见,我们需要做些尴尬的事…
amrnrhlw3#
Swift 5和iOS 13更新
以下函数允许您为滑动操作设置字体和色调。
要开始使用,请将扩展添加到
UITableView
。在委托中,将以下功能添加到
UITableViewDataSource.WillBeginEditing(UITableView, IndexPath)
方法中。根据需要替换字体和颜色参数。颜色是可选的。w7t8yxp54#
我发现了一个非常简单的方法,帮助我所有你要做的就是创建自己的视图,并添加一个标签,图像或任何你想要的
从表面上定制这个视图中的所有内容,然后使用下面的代码将其转换为图像接下来,将图像添加到操作按钮,就这样了
deleteAction.image = createDeleteImage()///这是我的函数,它创建了一个视图,然后使用下面的扩展名,它将其转换为一个图像
在执行此操作之前,不要忘记使按钮的标题等于空字符串“”
How to convert a UIView to an image
tzcvj98z5#
对于目标C
创建方法
此方法将从标签返回图像。
现在在tableview数据源方法中