iOS按钮底部自带下划线

2018-11-16  本文已影响9人  梦里桃花舞倾城

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

原文

[UITabBar appearance].selectionIndicatorImage = [UIImage new];
+ (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];
}
上一篇下一篇

猜你喜欢

热点阅读