【问题标题】:How to create a strike through UIButton?如何通过 UIButton 创建罢工?
【发布时间】:2015-01-14 11:02:55
【问题描述】:
是否可以在不使用图像的情况下创建带有删除线的按钮?
我知道 UILabel 可以这样做
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Your String here"];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
value:@2
range:NSMakeRange(0, [attributeString length])];
【问题讨论】:
标签:
ios
objective-c
uibutton
【解决方案1】:
您需要使用setAttributedTitle:forState: 传递您创建的属性字符串来设置按钮的标题。
【解决方案2】:
你可以试试
[button setAttributedTitle:attributeString forState:UIControlStateNormal];
[button setAttributedTitle:attributeString forState:UIControlStateHighlighted]
【解决方案3】:
let attributes = [NSStrikethroughStyleAttributeName : 1]
let title = NSAttributedString(string: "YOUR STRING", attributes: attributes)
button.setAttributedTitle(title, forState: .Normal)
【解决方案4】:
使用 Strike Through Attribute 创建一个可变属性字符串,并将此属性字符串设置为按钮的属性文本。使用了@grabury question上面的attributeString代码sn-p。
UIButton *button = [UIButton alloc] init];
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Your Text Here"];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
value:@2
range:NSMakeRange(0, [attributeString length])];
button.titleLabel.attributedText = attributeString;
谢谢。