当前位置: 首页 > 知识库问答 >
问题:

如何做自定义字体和颜色在UITableViewRowAction没有故事板

蓬威
2023-03-14

我有一个经典的TableView,你可以在这里删除项目,如果你滑动,然后点击按钮。我知道如何在单元格上设置自定义背景,但我找不到如何设置自定义字体和颜色。

谢谢你的帮助!

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]?  {

    var deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, 
                   title: "Delete", 
                   handler: { 
                      (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
                           println("Delete button clicked!")
                   })

    deleteAction.backgroundColor = UIColor.redColor()

    return [deleteAction]
}

共有3个答案

范飞翰
2023-03-14

如果使用XCode的调试视图层次结构查看当滑动按钮处于活动状态时UITableView中发生的情况,您将看到UITableViewRowAction项转换为名为\u UITableViewCellActionButton,包含在UITableViewCellDeleteConfirmationView中的按钮。更改按钮属性的一种方法是在将其添加到UITableViewCell时拦截它。在UITableViewCell派生类中,编写如下内容:

private let buttonFont = UIFont.boldSystemFontOfSize(13)
private let confirmationClass: AnyClass = NSClassFromString("UITableViewCellDeleteConfirmationView")!

override func addSubview(view: UIView) {
    super.addSubview(view)

    // replace default font in swipe buttons
    let s = subviews.flatMap({$0}).filter { $0.isKindOfClass(confirmationClass) }
    for sub in s {
        for button in sub.subviews {
            if let b = button as? UIButton {
                b.titleLabel?.font = buttonFont
            }
        }
    }
}
范甫
2023-03-14

我想分享我对ObjC的解决方案,这只是一个技巧,但对我来说是预期的。

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // this just convert view to `UIImage`
    UIImage *(^imageWithView)(UIView *) = ^(UIView *view) {

        UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    };

    // This is where the magic happen,
    // The width and height must be dynamic (it's up to you how to implement it)
    // to keep the alignment of the label in place
    //
    UIColor *(^getColorWithLabelText)(NSString*, UIColor*, UIColor*) = ^(NSString *text, UIColor *textColor, UIColor *bgColor) {

        UILabel *lbDelete = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 47, 40)];
        lbDelete.font = [UIFont boldSystemFontOfSize:11];
        lbDelete.text = text;
        lbDelete.textAlignment = NSTextAlignmentCenter;
        lbDelete.textColor = textColor;
        lbDelete.backgroundColor = bgColor;

        return [UIColor colorWithPatternImage:imageWithView(lbDelete)];
    };

    // The `title` which is `@"   "` is important it 
    // gives you the space you needed for the 
    // custom label `47[estimated width], 40[cell height]` on this example
    //
    UITableViewRowAction *btDelete;
    btDelete = [UITableViewRowAction
                rowActionWithStyle:UITableViewRowActionStyleDestructive
                title:@"   "
                handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
                    NSLog(@"Delete");
                    [tableView setEditing:NO];
                }];
    // Implementation
    //
    btDelete.backgroundColor = getColorWithLabelText(@"Delete", [UIColor whiteColor], [YJColor colorWithHexString:@"fe0a09"]);

    UITableViewRowAction *btMore;
    btMore   = [UITableViewRowAction
                rowActionWithStyle:UITableViewRowActionStyleNormal
                title:@"   "
                handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
                    NSLog(@"More");
                    [tableView setEditing:NO];
                }];
    // Implementation
    //
    btMore.backgroundColor = getColorWithLabelText(@"More", [UIColor darkGrayColor], [YJColor colorWithHexString:@"46aae8"]);

    return @[btMore, btDelete];
}

[YJColor的颜色和HexString:

王骏
2023-03-14

嗯,我发现设置自定义字体的唯一方法是使用UIAppearance协议的appearance方法中包含的appearance。该方法在Swift中还不可用,因此必须在Objective-C中使用。

我在实用工具Objective-C类中创建了一个类方法来设置它:

+ (void)setUpDeleteRowActionStyleForUserCell {

    UIFont *font = [UIFont fontWithName:@"AvenirNext-Regular" size:19];

    NSDictionary *attributes = @{NSFontAttributeName: font,
                      NSForegroundColorAttributeName: [UIColor whiteColor]};

    NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString: @"DELETE"
                                                                          attributes: attributes];

    /*
     * We include UIView in the containment hierarchy because there is another button in UserCell that is a direct descendant of UserCell that we don't want this to affect.
     */
    [[UIButton appearanceWhenContainedIn:[UIView class], [UserCell class], nil] setAttributedTitle: attributedTitle
                                                                                          forState: UIControlStateNormal];
}

这是可行的,但绝对不理想。如果在包含层次结构中不包含UIView,那么它最终也会影响披露指示器(我甚至没有意识到披露指示器是UIButton子类)。此外,如果单元格中的UIButton位于单元格的子视图中,则该按钮也会受到此解决方案的影响。

考虑到复杂性,最好只使用一个更可定制的开放源码库作为表格单元格滑动选项。

 类似资料:
  • 我正在使用IText7从html字符串生成pdf。现在,我需要对段落应用自定义颜色和自定义字体或字体系列。 如何使用Itext7实现这一点? 谢谢

  • 跟着这个答案https://stackoverflow.com/a/55113171/324969这个答案https://stackoverflow.com/a/60686826/324969. 如果我按下Ctrl,逗号,在设置中搜索“颜色”,单击settings.json中的

  • 我使用的是android CalendarView,我需要在日历上突出显示一些日期,找不到任何方法来这样做,是否有任何调整可用?如果我可以在本机日历上保存一些事件,那么CalendarView会突出显示这些日期吗?

  • 我想在我的应用程序android中使用一个自定义复选框,这个自定义复选框没有设置颜色的功能,在示例中,他们使用(http://schemas.android.com/apk/res-auto)喜欢(app)并设置颜色app:stroke_color=“#2196F3”我想知道如何通过编程设置颜色,链接自定义复选框https://github.com/lguipeng/AnimCheckBox

  • 我正在努力修改MUI next(v1)中的按钮颜色。 我该如何设置muitheme,使其行为与bootstrap相似,这样我就可以用“btn危险”表示红色,“btn成功”表示绿色? 我尝试了自定义,但它不能正常工作(悬停颜色不会改变),而且似乎是重复的。我有什么选择?

  • 颜色 主导颜色 灰阶 字体 字体字号 图标 气泡菜单 组件内容 基础形态 容器 操作选项 选择浮层 顶部浮层 底部浮层 侧边浮层 示例 布局 标签选项 <!-- # 顶部导航栏 原生顶部导航栏 示例 个性定制 示例 --> Toast 单行展示 双行展示 状态提示 Dialog 弹窗属性 页面示例 Snackbar <!-- # 结果页面 布局 页面示例 组成结构 --> 布局 页面示例 类型 页