Remove underline on UIButton in iOS 7

前端 未结 9 2188
北海茫月
北海茫月 2020-12-10 14:26

Any one know how to remove the UIButton underline that appears because of Accessibility?

(I know it\'s because the user turned on \"Button Shapes\")

相关标签:
9条回答
  • 2020-12-10 14:58

    Set background image to the button.

    [yourBtnHere setBackgroundImage:[[UIImage alloc] init] forState:UIControlStateNormal];
    
    0 讨论(0)
  • 2020-12-10 14:59

    Let me get this straight. Apple added an accessibility feature that lets users mark buttons with underlines if they want to.

    You want a way to defeat this feature, specifically designed to help people with handicaps use their devices, when the feature is something that the user has to ask for.

    Why?

    It is very likely not possible using standard buttons. If you did figure out a way to do it, Apple would likely reject your app because it defeats a system function meant to help the disabled.

    So the answer is: Don't do that.

    0 讨论(0)
  • 2020-12-10 15:00

    If button is underlined due accessibility button shape option then you could set button title by using image but not by text. Simply create image where text will be drawn and set it to the button. In this case iOS can't recognize text there and won't insert underline.
    You could consider it as hack but not as clear solution.

    0 讨论(0)
  • 2020-12-10 15:02

    The "Button shapes" is a new accessibility option in iOS 7.1. If the user want to have this option activated, there is nothing you can do. This is the user's choice.

    0 讨论(0)
  • 2020-12-10 15:06

    You can't turn off this accessibility feature.

    Make a custom UILabel or UIView with UITapGestureRecognizer if you really want to get rid of it.

    0 讨论(0)
  • 2020-12-10 15:13

    First get attribute string from button which has been set.

    NSMutableAttributedString *attrStr = [[yourBtnHere  attributedTitleForState:UIControlStateNormal] mutableCopy];
    

    Remove Attribute using removeAttribute like this :

    [attrStr removeAttribute:NSUnderlineStyleAttributeName range:NSMakeRange(0,[attrStr length])];
    [attrStr addAttribute: NSUnderlineStyleAttributeName value: [NSNumber numberWithInt:0] range: [attrStr length]];
    

    Reset attribute using addAttribute like this:

    UIColor *textBtncolor = [UIColor blackColor];
    [attrStr addAttribute:NSForegroundColorAttributeName value:textBtncolor range:NSMakeRange(0, attrStr.length)];
    

    Now set attribute string in your button

    [yourBtnHere setAttributedTitle:[attrStr copy] forState:UIControlStateNormal];
    
    0 讨论(0)
提交回复
热议问题