Mitul Bhadeshiya
Mitul Bhadeshiya

Reputation: 1358

Remove underline on UIButton in iOS 7

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?

Upvotes: 13

Views: 19466

Answers (7)

Larme
Larme

Reputation: 26096

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

Upvotes: 8

user4291543
user4291543

Reputation: 109

Set background image to the button.

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

Upvotes: 10

Duncan C
Duncan C

Reputation: 131503

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.

Upvotes: 17

Paresh Navadiya
Paresh Navadiya

Reputation: 38259

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];

Upvotes: 2

Laszlo
Laszlo

Reputation: 2778

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.

Upvotes: 2

Vlad Papko
Vlad Papko

Reputation: 13312

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.

Upvotes: 3

Emilie
Emilie

Reputation: 2413

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.

Upvotes: 4

Related Questions