如何更改UITableViewRowAction标题颜色?

在“editActionsForRowAtIndexPath”方法中使用UITableViewRowAction。 我可以更改UITableViewRowAction的背景颜色,但不能更改标题颜色。 但是我想改变UITableViewRowAction的颜色。 任何有关这方面的投入都是可观的。

恐怕没有办法改变UITableViewRowAction的标题颜色。

你可以改变的唯一的事情是:

  • 背景颜色
  • 风格(破坏性(红色backgroundcolor,…)
  • 标题

有关更多信息,请参阅Apple Doc UITableViewRowAction

有一种方法可以实现你正在寻找的东西。 但是,它有点棘手。

结果示例:

在这里输入图像说明

这个技巧的想法是,你可以实际修改背景颜色。 这意味着你可以设置UIColor的+ colorWithPatternImage:并设置一个匹配所需样式的位图图像。 这可以通过使用graphics编辑器创build图像或使用例如Core Graphics进行渲染来实现。 这个解决scheme唯一的问题是,你必须用你特定的文本属性模拟原始标题长度,以使其正常工作,并且你必须为表视图行操作设置标题为空白string,以便表视图单元格将准备足够为您的空间“自定义操作button”。 如果使用可变单元行,在photoshop中创build静态PNG资产可能不合适。

这是NSString的类别,它将创build一串空白空间,为您的自定义button创build空间,其次将生成将作为背景图案图像放置的位图图像。 对于参数,你必须设置原始标题的文本属性,基本上是@{NSFontAttributeName: [UIFont systemFontOfSize:18]} ,比你想要的文本属性。 也许有更好的办法来实现这一点:)

 @implementation NSString (WhiteSpace) - (NSString *)whitespaceReplacementWithSystemAttributes:(NSDictionary *)systemAttributes newAttributes:(NSDictionary *)newAttributes { NSString *stringTitle = self; NSMutableString *stringTitleWS = [[NSMutableString alloc] initWithString:@""]; CGFloat diff = 0; CGSize stringTitleSize = [stringTitle sizeWithAttributes:newAttributes]; CGSize stringTitleWSSize; NSDictionary *originalAttributes = systemAttributes; do { [stringTitleWS appendString:@" "]; stringTitleWSSize = [stringTitleWS sizeWithAttributes:originalAttributes]; diff = (stringTitleSize.width - stringTitleWSSize.width); if (diff <= 1.5) { break; } } while (diff > 0); return [stringTitleWS copy]; } @end 

第二个重要的部分是呈现可用作UITableViewRowAction的backgroundColor的模式图像的位图的代码。

 - (UIImage *)imageForTableViewRowActionWithTitle:(NSString *)title textAttributes:(NSDictionary *)attributes backgroundColor:(UIColor *)color cellHeight:(CGFloat)cellHeight { NSString *titleString = title; NSDictionary *originalAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:18]}; CGSize originalSize = [titleString sizeWithAttributes:originalAttributes]; CGSize newSize = CGSizeMake(originalSize.width + kSystemTextPadding + kSystemTextPadding, originalSize.height); CGRect drawingRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, cellHeight)); UIGraphicsBeginImageContextWithOptions(drawingRect.size, YES, [UIScreen mainScreen].nativeScale); CGContextRef contextRef = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(contextRef, color.CGColor); CGContextFillRect(contextRef, drawingRect); UILabel *label = [[UILabel alloc] initWithFrame:drawingRect]; label.textAlignment = NSTextAlignmentCenter; label.attributedText = [[NSAttributedString alloc] initWithString:title attributes:attributes]; [label drawTextInRect:drawingRect]; //This is other way how to render string // CGSize size = [titleString sizeWithAttributes:attributes]; // CGFloat x = (drawingRect.size.width - size.width)/2; // CGFloat y = (drawingRect.size.height - size.height)/2; // drawingRect.origin = CGPointMake(x, y); // [titleString drawInRect:drawingRect withAttributes:attributes]; UIImage *returningImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return returningImage; } 

然后当你创build你的行动时,你可以做这样的事情:

 NSDictionary *systemAttributes = @{ NSFontAttributeName: [UIFont systemFontOfSize:18] }; NSDictionary *newAttributes = @{NSFontAttributeName: [UIFont fontWithName:@"Any font" size:15]}; NSString *actionTitle = @"Delete"; NSString *titleWhiteSpaced = [actionTitle whitespaceReplacementWithSystemAttributes:systemTextFontAttributes newAttributes:newAttributes]; UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:titleWhiteSpaced handle:NULL]; UIImage *patternImage = [self imageForTableViewRowActionWithTitle:actionTitle textAttributes:newAttributes backgroundColor:[UIColor redColor] cellHeight:50]; rowAction.backgroundColor = [UIColor colorWithPatternImage:patternImage]; 

这个解决scheme显然是不好的,但是你可以在不使用私有API的情况下达到预期的效果,并且在将来如果有什么事情发生,这不会破坏你的应用程序。 只有button看起来不合适。

结果示例:

在这里输入图像说明

这个代码当然有很大的改进空间,任何build议,都表示赞赏。

迅速

没有必要弄乱UIButton.appearance …

把它放在你单元格的类中,并根据你的需要改变UITableViewCellActionButton。

 override func layoutSubviews() { super.layoutSubviews() for subview in self.subviews { for subview2 in subview.subviews { if (String(subview2).rangeOfString("UITableViewCellActionButton") != nil) { for view in subview2.subviews { if (String(view).rangeOfString("UIButtonLabel") != nil) { if let label = view as? UILabel { label.textColor = YOUR COLOUR } } } } } } } 

确实有一种方法来改变UITableViewRowAction的标题颜色。 这是一个button。 您可以使用UIButton外观代理:

[[UIButton appearance] setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];

在这里输入图像说明

Lee Andrew在Swift 3 / Swift 4中 的回答 :

 class MyCell: UITableViewCell { override func layoutSubviews() { super.layoutSubviews() for subview in self.subviews { for sub in subview.subviews { if String(describing: sub).range(of: "UITableViewCellActionButton") != nil { for view in sub.subviews { if String(describing: view).range(of: "UIButtonLabel") != nil { if let label = view as? UILabel { label.textColor = UIColor.black } } } } } } } } 

你可以在你的UITableViewCell子类中添加这两个函数,并调用setActionButtonsTitleColor函数来设置动作button的标题颜色。

 func setActionButtonsTitleColor(color: UIColor) { let actionButtons: [UIButton] = self.getActionButtons() for actionButton in actionButtons { actionButton.setTitleColor(color, for: .normal) } } func getActionButtons() -> [UIButton] { let actionButtons: [UIButton] = self.subviews.map { (view: UIView) -> [UIView] in return view.subviews } .joined() .filter { (view: UIView) -> Bool in return String(describing: view).contains("UITableViewCellActionButton") }.flatMap { (view: UIView) -> UIButton? in return view as? UIButton } return actionButtons } 
 -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"edit" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { // Action something here }]; editAction.backgroundColor = [UIColor whiteColor]; [[UIButton appearance] setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; return @[editAction];