工作生活

iOS 修改导航栏按钮字体及颜色的两种方式

2019-07-04  本文已影响0人  sergeant
  1. 使用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;
}
  1. 使用自定义视图
+ (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];
}

上一篇 下一篇

猜你喜欢

热点阅读