SWIFT 5 UPDATE CODE个
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSRange(location: 0, length: attributeString.length))
然后:
yourLabel.attributedText = attributeString
To make some part of string to strike then provide range
let somePartStringRange = (yourStringHere as NSString).range(of: "Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: somePartStringRange)
Objective-C个
在iOS 6.0 >中UILabel
支承座NSAttributedString
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Your String here"];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
value:@2
range:NSMakeRange(0, [attributeString length])];
Swift个
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your String here")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
Definition:
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)aRange
Parameters List:
个
name:指定属性名称的字符串.属性键可以由另一个框架提供,也可以是您定义的自定义键.有关在何处查找系统提供的属性键的信息,请参阅NSAttributedString类参考中的概述部分.
value:与名称关联的属性值.
aRange:指定属性/值对应用的字符范围.
然后
yourLabel.attributedText = attributeString;
对于lesser than iOS 6.0 versions
,你需要3-rd party component
才能做到这一点.
其中一个是TTTAttributedLabel,另一个是OHAttributedLabel.