gpt4 book ai didi

ios - 如何更改 UITableViewRowAction 标题颜色?

转载 作者:IT王子 更新时间:2023-10-29 08:21:50 27 4
gpt4 key购买 nike

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

最佳答案

有一种方法可以实现您的目标。但这有点棘手。

结果示例:

enter image description here

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

这是 NSString 的类别,它创建空白字符串,为您的自定义按钮创建空间,第二个将生成将作为背景图案图像放置的位图图像。对于参数,您必须为原始标题设置文本属性,基本上是 @{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 的背景颜色的图案图像。

- (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;
}

然后当您创建行操作时,您可以执行如下操作:

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];

这个解决方案显然是 hacky,但您可以在不使用私有(private) API 的情况下获得预期的结果,并且将来如果出现问题,这不会破坏您的应用程序。只有按钮看起来不合适。

结果示例:

enter image description here

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

关于ios - 如何更改 UITableViewRowAction 标题颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26887563/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com