Remove underline on UIButton in iOS 7

会有一股神秘感。 提交于 2019-11-27 06:04:40

问题


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

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


回答1:


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.




回答2:


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.




回答3:


You should go to Settings > General > Accessibility and turn on/off Button Shapes and then revisit the app in question. You should only see underlines and shapes when this control is on, and this is to indicate what buttons that look like text are actually tappable for those with accessibility needs.




回答4:


Set background image to the button.

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



回答5:


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.




回答6:


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.




回答7:


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.




回答8:


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



回答9:


To remove the unwanted Blue colour layer from UIButton in simulator

go to Settings > General > Accessibility and turn off Button Shapes and then reRun the app it will remove the unwanted blue layer.



来源:https://stackoverflow.com/questions/23246543/remove-underline-on-uibutton-in-ios-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!