- 使用tintColor修改颜色,使用setTitleTextAttributes: forState:修改字体
+ (instancetype)itemWithTitle:(NSString *)text withColor:(UIColor *)color withFont:(UIFont *)font target:(id)target action:(SEL)action {
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:text style:UIBarButtonItemStylePlain target:target action:action];
item.tintColor = color;
[item setTitleTextAttributes:@{NSFontAttributeName : font} forState:UIControlStateNormal];
[item setTitleTextAttributes:@{NSFontAttributeName : font} forState:UIControlStateSelected];
return item;
}
- 使用自定义视图
+ (instancetype)itemWithTitle:(NSString *)text withColor:(UIColor *)color withFont:(UIFont *)font target:(id)target action:(SEL)action {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:text forState:UIControlStateNormal];
[button setTitleColor:color forState:UIControlStateNormal];
[button setTitleColor:UIColor.lightTextColor forState:UIControlStateHighlighted];
button.titleLabel.font = font;
[button sizeToFit];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return [[UIBarButtonItem alloc] initWithCustomView:button];
}
网友评论