如何创建带有删除线文本的 UILabel? [英] How can I create a UILabel with strikethrough text?
问题描述
我想创建一个UILabel
,里面的文字是这样的
I want to create a UILabel
in which the text is like this
我该怎么做?文字小,线条也要小.
How can I do this? When the text is small, the line should also be small.
推荐答案
SWIFT 5 更新代码
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSRange(location: 0, length: attributeString.length))
然后:
yourLabel.attributedText = attributeString
使字符串的某些部分敲击然后提供范围
let somePartStringRange = (yourStringHere as NSString).range(of: "Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: somePartStringRange)
目标 C
在 iOS 6.0 > UILabel
支持 NSAttributedString
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Your String here"];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
value:@2
range:NSMakeRange(0, [attributeString length])];
迅捷
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your String here")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
定义:
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)aRange
参数列表:
name :指定属性名称的字符串.属性键可以由另一个框架提供,也可以是您定义的自定义键.有关在哪里可以找到系统提供的属性键的信息,请参阅 NSAttributedString 类参考中的概述部分.
name : A string specifying the attribute name. Attribute keys can be supplied by another framework or can be custom ones you define. For information about where to find the system-supplied attribute keys, see the overview section in NSAttributedString Class Reference.
value :与名称关联的属性值.
value : The attribute value associated with name.
aRange :指定属性/值对适用的字符范围.
aRange : The range of characters to which the specified attribute/value pair applies.
然后
yourLabel.attributedText = attributeString;
对于低于 iOS 6.0 的版本
,您需要 3-rd party component
来执行此操作.其中一个是TTTAttributedLabel,另一个是OHAttributedLabel.
For lesser than iOS 6.0 versions
you need 3-rd party component
to do this.
One of them is TTTAttributedLabel, another is OHAttributedLabel.
这篇关于如何创建带有删除线文本的 UILabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!