ios 如何创建带有删除线文本的 UILabel?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13133014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create a UILabel with strikethrough text?
提问by Dev
I want to create a UILabel
in which the text is like this
我想创建一个UILabel
文本是这样的
How can I do this? When the text is small, the line should also be small.
我怎样才能做到这一点?当文字很小时,线条也应该很小。
回答by Paresh Navadiya
SWIFT 4 UPDATE CODE
SWIFT 4 更新代码
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
then:
然后:
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
目标-C
In iOS 6.0 >UILabel
supports NSAttributedString
在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:
Parameters List:
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.
name:指定属性名称的字符串。属性键可以由另一个框架提供,也可以是您定义的自定义键。有关在哪里可以找到系统提供的属性键的信息,请参阅 NSAttributedString 类参考中的概述部分。
value: The attribute value associated with name.
value:与名称关联的属性值。
aRange: The range of characters to which the specified attribute/value pair applies.
aRange:指定的属性/值对适用的字符范围。
Then
然后
yourLabel.attributedText = attributeString;
For lesser than iOS 6.0 versions
you need 3-rd party component
to do this.
One of them is TTTAttributedLabel, another is OHAttributedLabel.
因为lesser than iOS 6.0 versions
你需3-rd party component
要这样做。其中一个是TTTAttributedLabel,另一个是OHAttributedLabel。
回答by Chris Trevarthen
In Swift, using the enum for single strikethrough line style:
在 Swift 中,使用枚举作为单个删除线样式:
let attrString = NSAttributedString(string: "Label Text", attributes: [NSStrikethroughStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue])
label.attributedText = attrString
Additional strikethrough styles (Remember to access the enum using .rawValue):
其他删除线样式(请记住使用 .rawValue 访问枚举):
- NSUnderlineStyle.StyleNone
- NSUnderlineStyle.StyleSingle
- NSUnderlineStyle.StyleThick
- NSUnderlineStyle.StyleDouble
- NSUnderlineStyle.StyleNone
- NSUnderlineStyle.StyleSingle
- NSUnderlineStyle.StyleThick
- NSUnderlineStyle.StyleDouble
Strikethrough patterns (to be OR-ed with the style):
删除线模式(与样式进行 OR 运算):
- NSUnderlineStyle.PatternDot
- NSUnderlineStyle.PatternDash
- NSUnderlineStyle.PatternDashDot
- NSUnderlineStyle.PatternDashDotDot
- NSUnderlineStyle.PatternDot
- NSUnderlineStyle.PatternDash
- NSUnderlineStyle.PatternDashDot
- NSUnderlineStyle.PatternDashDotDot
Specify that the strikethrough should only be applied across words (not spaces):
指定删除线应仅应用于单词(而不是空格):
- NSUnderlineStyle.ByWord
- NSUnderlineStyle.ByWord
回答by Kjuly
I prefer NSAttributedString
rather than NSMutableAttributedString
for this simple case:
我更喜欢NSAttributedString
而不是NSMutableAttributedString
这个简单的案例:
NSAttributedString * title =
[[NSAttributedString alloc] initWithString:@"8"
attributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle)}];
[label setAttributedText:title];
Constants for specifying both the NSUnderlineStyleAttributeName
and NSStrikethroughStyleAttributeName
attributes of an attributed string:
用于指定属性字符串的NSUnderlineStyleAttributeName
和NSStrikethroughStyleAttributeName
属性的常量:
typedef enum : NSInteger {
NSUnderlineStyleNone = 0x00,
NSUnderlineStyleSingle = 0x01,
NSUnderlineStyleThick = 0x02,
NSUnderlineStyleDouble = 0x09,
NSUnderlinePatternSolid = 0x0000,
NSUnderlinePatternDot = 0x0100,
NSUnderlinePatternDash = 0x0200,
NSUnderlinePatternDashDot = 0x0300,
NSUnderlinePatternDashDotDot = 0x0400,
NSUnderlineByWord = 0x8000
} NSUnderlineStyle;
回答by Purnendu roy
Strikethrough in Swift 5.0
Swift 5.0 中的删除线
let attributeString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle,
value: NSUnderlineStyle.single.rawValue,
range: NSMakeRange(0, attributeString.length))
self.yourLabel.attributedText = attributeString
It worked for me like a charm.
它对我来说就像一种魅力。
Use it as extension
将其用作扩展
extension String {
func strikeThrough() -> NSAttributedString {
let attributeString = NSMutableAttributedString(string: self)
attributeString.addAttribute(
NSAttributedString.Key.strikethroughStyle,
value: NSUnderlineStyle.single.rawValue,
range:NSMakeRange(0,attributeString.length))
return attributeString
}
}
Call like this
像这样打电话
myLabel.attributedText = "my string".strikeThrough()
UILabel extension for strikethrough Enable/Disable.
删除线启用/禁用的 UILabel 扩展。
extension UILabel {
func strikeThrough(_ isStrikeThrough:Bool) {
if isStrikeThrough {
if let lblText = self.text {
let attributeString = NSMutableAttributedString(string: lblText)
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0,attributeString.length))
self.attributedText = attributeString
}
} else {
if let attributedStringText = self.attributedText {
let txt = attributedStringText.string
self.attributedText = nil
self.text = txt
return
}
}
}
}
Use it like this :
像这样使用它:
yourLabel.strikeThrough(btn.isSelected) // true OR false
回答by pekpon
SWIFT CODE
SWIFT代码
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
then:
然后:
yourLabel.attributedText = attributeString
Thanks to Prince answer;)
感谢王子回答;)
回答by Muhammad Asyraf
SWIFT 4
快速 4
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text Goes Here")
attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0, attributeString.length))
self.lbl_productPrice.attributedText = attributeString
Other method is to used String Extension
Extension
其他方法是使用字符串扩展
扩展
extension String{
func strikeThrough()->NSAttributedString{
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: self)
attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0, attributeString.length))
return attributeString
}
}
Calling the function :Used it like so
调用函数:像这样使用它
testUILabel.attributedText = "Your Text Goes Here!".strikeThrough()
Credit to @Yahya- update Dec 2017
Credit to @kuzdu- update Aug 2018
归功于@Yahya - 2017 年 12 月更新
归功于 @kuzdu - 2018 年 8 月更新
回答by Ameet Dhas
You can do it in IOS 6 using NSMutableAttributedString.
您可以使用 NSMutableAttributedString 在 IOS 6 中做到这一点。
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc]initWithString:@"8"];
[attString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(0,[attString length])];
yourLabel.attributedText = attString;
回答by Karthickkck
Strike out UILabel text in Swift iOS. PLease try this it's working for me
在 Swift iOS 中删除 UILabel 文本。请试试这个它对我有用
let attributedString = NSMutableAttributedString(string:"12345")
attributedString.addAttribute(NSAttributedStringKey.baselineOffset, value: 0, range: NSMakeRange(0, attributedString.length))
attributedString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSNumber(value: NSUnderlineStyle.styleThick.rawValue), range: NSMakeRange(0, attributedString.length))
attributedString.addAttribute(NSAttributedStringKey.strikethroughColor, value: UIColor.gray, range: NSMakeRange(0, attributedString.length))
yourLabel.attributedText = attributedString
You can change your "strikethroughStyle" like styleSingle, styleThick,styleDouble
回答by Vladimir Pchelyakov
Swift 5
斯威夫特 5
extension String {
/// Apply strike font on text
func strikeThrough() -> NSAttributedString {
let attributeString = NSMutableAttributedString(string: self)
attributeString.addAttribute(
NSAttributedString.Key.strikethroughStyle,
value: 1,
range: NSRange(location: 0, length: attributeString.length))
return attributeString
}
}
Example:
例子:
someLabel.attributedText = someText.strikeThrough()
回答by Micro
For anyone looking on how to do this in a tableview cell (Swift) you have to set the .attributeText like this:
对于任何想在 tableview 单元格 (Swift) 中执行此操作的人,您必须像这样设置 .attributeText:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("TheCell")!
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: message)
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
cell.textLabel?.attributedText = attributeString
return cell
}
If you want to remove the strikethrough do this otherwise it will stick around!:
如果您想删除删除线,请执行此操作,否则它会一直存在!:
cell.textLabel?.attributedText = nil