iOS按钮底部自带下划线

无意中测试发现,手机上所有的文本按钮,文字都自带下划线! 瞬间懵逼。啥玩意? 底部TabBar自带背景色 。 百度了一下原来是手机设置的问题。

原文

  • UITabbar
[UITabBar appearance].selectionIndicatorImage = [UIImage new];
  • UIButtonbutton添加一个分类修改富文本样式即可
+ (void)load {
    //只执行一次这个方法
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [UIButton changeFontBottomLine];
    });
}
#pragma mark - Change Font Bottom Line
 + (void)changeFontBottomLine {
    Class class = [self class];
    
    SEL originalSelector = @selector(setTitle:forState:);
    SEL swizzledSelector = @selector(CKSetTitle:forState:);
    
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    
    BOOL didAddMethod =
    class_addMethod(class,
                    originalSelector,
                    method_getImplementation(swizzledMethod),
                    method_getTypeEncoding(swizzledMethod));
    
    if (didAddMethod) {
        class_replaceMethod(class,
                            swizzledSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
        
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}
 - (void)CKSetTitle:(NSString *)title forState:(UIControlState)state {
    [self CKSetTitle:title forState:state];
    NSDictionary *attrbiteDic = @{
                                  NSFontAttributeName:self.titleLabel.font,
                                  NSForegroundColorAttributeName:[self titleColorForState:state],
                                  NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleNone]
                                  };
    
    self.titleLabel.attributedText = [[NSAttributedString alloc] initWithString:title attributes:attrbiteDic];
}

你可能感兴趣的:(iOS按钮底部自带下划线)