Question

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")

enter image description here

enter image description here

How can I remove that programmatically, or by setting some property in Xcode?

Was it helpful?

Solution 3

Check below code :

NSMutableAttributedString *attrStr = [[yourBtnHere attributedTitleForState:UIControlStateNormal] mutableCopy];//or whatever the state you want
[attrStr enumerateAttributesInRange:NSMakeRange(0, [attrStr length])
                            options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
                         usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop)
{
    NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes];
    [mutableAttributes removeObjectForKey:NSUnderlineStyleAttributeName];
    [attrStr setAttributes:mutableAttributes range:range];
}];

• With the inspector/IB: Select your UIButton.
Show the Attributes Inspector.
The Textsettings should be in Attributed. Select the text, click on the fond item remove the Underlining setting it at none.
enter image description here

OTHER TIPS

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.

Set background image to the button.

[yourBtnHere setBackgroundImage:[[UIImage alloc] init] forState:UIControlStateNormal];

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.

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.

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.

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top