![](/img/trans.png)
[英]How to give custom width to UIContextualAction in TableView (swipe)
[英]Set custom font for UITableView swipe action (UIContextualAction)
如何在UIContextualAction
為標題設置自定義字體?
我試過UIAppearance
但沒有任何運氣......
干杯! :)
我找到了一種通過使用圖像屬性而不是標題來做到這一點的方法......
標准字體(刪除/重命名)
自定義字體(刪除/重命名)
要創建標簽圖像,我有這個擴展:
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])
}
我最近找到了一種方法,通過使用按鈕 titleLabel 而不是圖像屬性來執行此操作,這樣您就可以保持對文本和圖像進行操作的能力。
正如你將看到的,我們需要做一些尷尬的事情......
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)
}
}
}
}
}
以下功能允許您為滑動操作設置字體和色調。
首先,向UITableView
添加一個擴展。
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
}
}
}
}
}
在您的委托中,將以下功能添加到您的UITableViewDataSource.WillBeginEditing(UITableView, IndexPath)
方法中。 根據需要替換字體和顏色參數。 色調顏色是可選的。
self?.tableView.setSwipeActionFont(.systemFont(ofSize: 24.0, withTintColor: .systemRed)
對於目標 C
創建方法
- (UIImage *)createImageFromLabel:(UILabel *)label
{
UIGraphicsBeginImageContext(label.bounds.size);
[label.layer renderInContext:UIGraphicsGetCurrentContext()];;
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
此方法將從您的標簽返回圖像。
現在在 tableview 數據源方法中
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIContextualAction *action = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:nil handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
//Your code here
}];
action.backgroundColor = [UIColor redColor];
// Create label as you want view for delete button
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont systemFontSize:14];
label.text = @"Your Text";
label.textColor = [UIColor whiteColor];
[label sizeToFit];
UIImage *image = [self createImageFromLabel:label];
action.image = image;
UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[action]];
return actions;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.