删除iOS 7中的UIButton的下划线

任何人知道如何删除由于辅助function出现的UIButton下划线?

(我知道这是因为用户打开“button形状”)

在这里输入图像说明

在这里输入图像说明

我怎样才能以编程方式删除它,或者通过在Xcode中设置一些属性?

检查下面的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]; }]; 

•使用检查员/ IB:select您的UIButton
显示Attributes Inspector
Text设置应该在Attributed 。 select文本,点击全部项目删除下划线设置它none
在这里输入图像说明

让我说清楚。 苹果增加了一个辅助function,让用户在需要的时候用下划线标记button。

您想要一种方法来打败这个function,该function专门devise用来帮助有障碍的人使用他们的设备,当function是用户不得不要求的function时。

为什么?

这很可能不可能使用标准button。 如果你确实想出了一个办法,苹果可能会拒绝你的应用程序,因为它击败了一个旨在帮助残疾人的系统function。

所以答案是:不要那样做。

你应该去设置>常规>辅助function,打开/closuresbutton形状,然后重新访问有问题的应用程序。 当这个控件打开的时候,你只应该看到下划线和形状,这是为了表示看起来像文本的那些button实际上可以用于那些具有可访问性需求的button。

将背景图像设置为button。

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

“button形状”是iOS 7.1中的一个新的可访问选项。 如果用户想要激活这个选项,那么你不能做任何事情。 这是用户的select。

如果button带有可见性button形状选项,则可以使用图像设置button标题,但不能使用文本设置。 只需在绘制文本的位置创build图像,并将其设置为button即可。 在这种情况下,iOS无法识别文本,不会插入下划线。
你可以认为它是黑客,但不是明确的解决scheme。

您无法closures此辅助function。

如果你真的想摆脱它, 使用UITapGestureRecognizer自定义UILabel或UIView 。

首先从已设置的button获取attribute string

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

使用removeAttribute像这样去除属性:

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

使用addAttribute重置属性,如下所示:

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

现在在你的button中设置属性string

 [yourBtnHere setAttributedTitle:[attrStr copy] forState:UIControlStateNormal]; 

在模拟器中从UIButton中移除不需要的蓝色图层

去“设置”>“常规”>“辅助function”,然后closures“button形状”,然后重新运行应用程序,它将删除不需要的蓝色图层。