Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何更改UITableViewRowAction标题颜色?_Ios_Objective C_Uitableview_Ios8_Uitableviewrowaction - Fatal编程技术网

Ios 如何更改UITableViewRowAction标题颜色?

Ios 如何更改UITableViewRowAction标题颜色?,ios,objective-c,uitableview,ios8,uitableviewrowaction,Ios,Objective C,Uitableview,Ios8,Uitableviewrowaction,我在“EditActionsErrorWatIndeXPath”方法中使用UITableViewRowAction。我可以更改UITableViewRowAction的背景颜色,但无法更改标题颜色。但是我想更改UITableViewRowAction的颜色。这方面的任何输入都将是可观的。恐怕无法更改UITableViewRowAction的标题颜色 您可以在操作中更改的唯一内容是: 背景色 样式(破坏性(红色背景色,…) 头衔 欲了解更多信息,请参阅有一种方法可以实现您所寻找的目标。但这并不

我在“EditActionsErrorWatIndeXPath”方法中使用UITableViewRowAction。我可以更改UITableViewRowAction的背景颜色,但无法更改标题颜色。但是我想更改UITableViewRowAction的颜色。这方面的任何输入都将是可观的。

恐怕无法更改UITableViewRowAction的标题颜色

您可以在操作中更改的唯一内容是:

  • 背景色
  • 样式(破坏性(红色背景色,…)
  • 头衔

欲了解更多信息,请参阅

有一种方法可以实现您所寻找的目标。但这并不难

结果示例:

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

这是NSString的类别,它创建一个空空格字符串,该字符串将为自定义按钮创建空间,第二个将生成位图图像,该图像将作为背景图案图像放置。对于参数,您必须为原始标题设置文本属性,这基本上是
@{NSFontAttributeName:[UIFont systemFontOfSize:18]}
,而不是您想要的文本属性。也许有更好的方法来实现这一点:)

然后,在创建行操作时,可以执行以下操作:

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]; 
extension UITableViewCell {

    /// Returns label of cell action button.
    ///
    /// Use this property to set cell action button label color.
    var cellActionButtonLabel: UILabel? {
        for subview in self.superview?.subviews ?? [] {
            if String(describing: subview).range(of: "UISwipeActionPullView") != nil {
                for view in subview.subviews {
                    if String(describing: view).range(of: "UISwipeActionStandardButton") != nil {
                        for sub in view.subviews {
                            if let label = sub as? UILabel {
                                return label
                            }
                        }
                    }
                }
            }
        }
        return nil
    }

}
这个解决方案显然是有漏洞的,但您可以在不使用私有API的情况下实现所需的结果,并且在将来如果出现问题,这不会破坏您的应用程序。只有按钮看起来不合适

结果示例:


当然,这段代码还有很大的改进空间,欢迎您提出任何建议。

确实有办法更改UITableViewRowAction的标题颜色。这是一个按钮。您可以使用
ui按钮
外观代理:


[[UIButton外观]设置标题颜色:[UIColor orangeColor]用于状态:UIControlStateNormal]

斯威夫特

不需要弄乱UIButton。外观

将其放入单元格的类中,并根据需要更改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
                        }

                    }
                }
            }
        }
    }

}
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
函数来设置操作按钮的标题颜色

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
}

iOS 11解决方案:

创建
UITableViewCell
扩展,如下所示:

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]; 
extension UITableViewCell {

    /// Returns label of cell action button.
    ///
    /// Use this property to set cell action button label color.
    var cellActionButtonLabel: UILabel? {
        for subview in self.superview?.subviews ?? [] {
            if String(describing: subview).range(of: "UISwipeActionPullView") != nil {
                for view in subview.subviews {
                    if String(describing: view).range(of: "UISwipeActionStandardButton") != nil {
                        for sub in view.subviews {
                            if let label = sub as? UILabel {
                                return label
                            }
                        }
                    }
                }
            }
        }
        return nil
    }

}
并将其写入您的
UITableViewCell

override func layoutSubviews() {
    super.layoutSubviews()
    cellActionButtonLabel?.textColor = .red
}

但是有一个错误-如果你快速拉动单元格,此代码有时不会改变颜色。

如果有人仍在寻找替代解决方案:

您可以使用swipecellkit吊舱


这使您可以自定义标签颜色、图像颜色甚至整个背景,同时保持本机实现的实际外观。

因此,在iOS 13天内,仍然没有公共api来更改
textColor
font
单元格操作

Swift 5.3、iOS 14的工作解决方案 来自线程中答案的黑客有非常不可靠的结果,但我已经设法让我的版本的黑客工作

1.获取标签 以下是我访问操作的简化方式
UILabel

扩展UITableViewCell{ var cellActionButtonLabel:UILabel{ 超级视图?子视图 .filter{String(说明:$0).range(of:“UISweepActionPullView”)!=nil} .flatMap{$0.subviews} .filter{String(说明:$0).range(of:“UISwipeActionStandardButton”)!=nil} .flatMap{$0.subviews} .compactMap{$0 as?UILabel}.first } }
2.在布局更改时更新标签 接下来,在我的
UITableViewCell
子类中重写
layoutSubviews()
是不够的,因此我还必须重写
layoutfneed()
,才能让黑客工作。 请注意,重要的是同时覆盖它们

覆盖函数布局子视图(){
super.layoutSubviews()
cellActionButtonLabel?.textColor=.black//您的颜色显示在这里
}
重写函数layoutifneed(){
super.layoutIfNeeded()
cellActionButtonLabel?.textColor=.black//您的颜色显示在这里
}
3.触发额外布局刷新 最后一个难题是安排额外的颜色刷新,因此我们涵盖了由于某种原因无法调用上述函数的所有情况。执行此操作的最佳位置是
UITableViewDelegate
method
…,willBeginEditingRowAt:…

func tableView(tableView:UITableView,willBeginEditingRowAt indexath:indexPath){
DispatchQueue.main.asyncAfter(截止日期:.now()+0.01){
tableView.cellForRow(at:indexPath)?.layoutIfNeeded()
}
}
延迟布局刷新触发器的作用是让UIKit首先配置单元,这样我们就可以在定制之后立即跳转
0.01
对我来说已经足够了,但我可能会因情况而异

4.利润 现在,您可以根据需要定制标签!更改文本、其颜色、字体或添加子视图

不言而喻,如果苹果公司决定改变他们的p
override func layoutSubviews() {
    super.layoutSubviews()
    cellActionButtonLabel?.textColor = .red
}