有人知道如何删除因辅助功能而出现的
UIButton
下划线吗?
(我知道是因为用户打开了“Button Shapes”)
如何以编程方式或通过在 Xcode 中设置某些属性来删除它?
让我直截了当地说。 Apple 添加了一项辅助功能,允许用户在需要时用下划线标记按钮。
你想要一种方法来打败这个功能,专门设计来帮助有障碍的人使用他们的设备,当这个功能是用户必须要求的东西。
为什么?
使用标准按钮很可能是不可能的。如果你确实想出了办法,Apple 可能会拒绝你的应用程序,因为它破坏了旨在帮助残疾人的系统功能。
所以答案是:不要那样做。
为按钮设置背景图片。
[yourBtnHere setBackgroundImage:[[UIImage alloc] init] forState:UIControlStateNormal];
检查下面
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: 选择您的
UIButton
.Attributes Inspector
.Text
设置应该在Attributed
。选择文本,单击喜欢的项目删除下划线设置在none
.“按钮形状”是 iOS 7.1 中新的辅助功能选项。如果用户想要激活此选项,您无能为力。这是用户的选择。
如果按钮由于可访问性按钮形状选项而带有下划线,那么您可以使用图像而不是文本来设置按钮标题。只需在将要绘制文本的位置创建图像并将其设置为按钮即可。在这种情况下,iOS 无法识别那里的文本,也不会插入下划线。
您可以将其视为 hack 但不是明确的解决方案。
您无法关闭此辅助功能。
如果您真的想摆脱它,请使用 UITapGestureRecognizer 自定义 UILabel 或 UIView。
首先从已设置的按钮中获取
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)];
现在在你的按钮中设置属性字符串
[yourBtnHere setAttributedTitle:[attrStr copy] forState:UIControlStateNormal];