美文网首页工作生活
iOS 修改导航栏按钮字体及颜色的两种方式

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

作者: sergeant | 来源:发表于2019-07-04 09:36 被阅读0次
    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];
    }
    
    

    相关文章

      网友评论

        本文标题:iOS 修改导航栏按钮字体及颜色的两种方式

        本文链接:https://www.haomeiwen.com/subject/vlakhctx.html