我有经典的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]
}
答案 0 :(得分:11)
嗯,我发现设置自定义字体的唯一方法是使用appearanceWhenContainedIn
协议的UIAppearance
方法。这个方法在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,那么该按钮也会受到此解决方案的影响。
考虑到复杂性,最好只使用其中一个可自定义的开源库来表格单元格滑动选项。
答案 1 :(得分:3)
我想与 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 colorWithHexString:<NSString>];
只是将十六进制字符串转换为UIColor。
答案 2 :(得分:2)
如果您使用XCode的调试视图层次结构来查看滑动按钮处于活动状态时UITableView中发生的情况,您将看到UITableViewRowAction项目转换为_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
}
}
}
}
答案 3 :(得分:0)
这似乎有效,至少在设置字体颜色方面是这样的:
- (void)setupRowActionStyleForTableViewSwipes {
UIButton *appearanceButton = [UIButton appearanceWhenContainedInInstancesOfClasses:@[[NSClassFromString(@"UITableViewCellDeleteConfirmationView") class]]];
[appearanceButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
}
答案 4 :(得分:0)
您可以使用UIButton.appearance
在行操作中设置按钮的样式。像这样:
let buttonStyle = UIButton.appearance(whenContainedInInstancesOf: [YourViewController.self])
let font = UIFont(name: "Custom-Font-Name", size: 16.0)!
let string = NSAttributedString(string: "BUTTON TITLE", attributes: [NSAttributedString.Key.font : font, NSAttributedString.Key.foregroundColor : UIColor.green])
buttonStyle.setAttributedTitle(string, for: .normal)
注意:这会影响此视图控制器中的所有按钮。
答案 5 :(得分:-1)
以下是一些可能有用的Swift代码:
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) ->[AnyObject]? {
let attributes = [NSFontAttributeName: UIFont.systemFontOfSize(UIFont.systemFontSize())] as Dictionary!
UIButton.appearance().setAttributedTitle(NSAttributedString(string: "Your Button", attributes: attributes), forState: .Normal)
// Things you do...
}
这将操纵应用程序中的所有按钮。
答案 6 :(得分:-1)
我认为您可以使用此方法仅在一个(或更多,您可以定义)viewcontrollers中更改外观:
//create your attributes however you want to
let attributes = [NSFontAttributeName: UIFont.systemFontOfSize(UIFont.systemFontSize())] as Dictionary!
//Add more view controller types in the []
UIButton.appearanceWhenContainedInInstancesOfClasses([ViewController.self])
希望这会有所帮助。
答案 7 :(得分:-1)
//The following code is in Swift3.1
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
{
let rejectAction = TableViewRowAction(style: UITableViewRowActionStyle.default, title: "\u{2715}\nReject") { action, indexPath in
print("didtapReject")
}
rejectAction.backgroundColor = UIColor.gray
let approveAction = TableViewRowAction(style: UITableViewRowActionStyle.default, title: "\u{2713}\nApprove") { action, indexPath in
print("didtapApprove")
}
approveAction.backgroundColor = UIColor.orange
return [rejectAction, approveAction]
}
答案 8 :(得分:-16)
这很简单。
那就是它!您所需要的只是按照其名称使用字体
cell.textLabel.font = [UIFont fontWithName:@"FontName" size:16];
更容易。你所需要的只是
cell.textlabel.textcolor = UIColor.redColor()
在您的情况下,您想要更改RowAction的字体。所以我想到的只有两个解决方案。一个使用[UIColor colorWithPatterImage:]
或者您可以使用[[UIButton appearance] setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
,因为RowAction包含一个按钮。