我想创建一个UILabel
文本是这样的
我怎样才能做到这一点?当文本很小时,行也应该很小。
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)
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])];
迅速
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
Parameters List:
name:指定属性名称的字符串。属性键可以由另一个框架提供,也可以是您定义的自定义键。有关在何处查找系统提供的属性键的信息,请参阅 NSAttributedString 类参考中的概述部分。
value:与 name 关联的属性值。
aRange:指定属性/值对适用的字符范围。
然后
yourLabel.attributedText = attributeString;
因为lesser than iOS 6.0 versions
你需3-rd party component
要这样做。其中一个是TTTAttributedLabel,另一个是OHAttributedLabel。
在 Swift 中,使用单删除线样式:
let attributedText = NSAttributedString(
string: "Label Text",
attributes: [.strikethroughStyle: NSUnderlineStyle.single.rawValue]
)
label.attributedText = attributedText
其他删除线样式(请记住使用 .rawValue):
.none
.single
.thick
.double
删除线模式(与样式进行 OR-ed):
.patternDot
.patternDash
.patternDashDot
.patternDashDotDot
指定删除线仅应用于单词(而不是空格):
.byWord
我更喜欢NSAttributedString
而不是NSMutableAttributedString
这个简单的案例:
NSAttributedString * title =
[[NSAttributedString alloc] initWithString:@"$198"
attributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle)}];
[label setAttributedText:title];
用于指定属性字符串的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;
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
它对我来说就像一种魅力。
使用它作为扩展
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
}
}
像这样打电话
myLabel.attributedText = "my string".strikeThrough()
删除线启用/禁用的 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
}
}
}
}
像这样使用它:
yourLabel.strikeThrough(btn.isSelected) // true OR false
SWIFT代码
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
然后:
yourLabel.attributedText = attributeString
感谢王子的回答;)
斯威夫特 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
其他方法是使用字符串扩展
扩展
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
}
}
调用函数:像这样使用它
testUILabel.attributedText = "Your Text Goes Here!".strikeThrough()
归功于@Yahya - 2017 年 12 月更新
归功于 @kuzdu - 2018 年 8 月更新
在 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
您可以使用 NSMutableAttributedString 在 IOS 6 中执行此操作。
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc]initWithString:@"$198"];
[attString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(0,[attString length])];
yourLabel.attributedText = attString;
斯威夫特 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
}
}
例子:
someLabel.attributedText = someText.strikeThrough()
对于任何想在 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
}
如果要删除删除线,请执行此操作,否则它会一直存在!:
cell.textLabel?.attributedText = nil
斯威夫特 4.2
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: product.price)
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attributeString.length))
lblPrice.attributedText = attributeString
Swift 5 - 短版
let attributedText = NSAttributedString(
string: "Label Text",
attributes: [.strikethroughStyle: NSUnderlineStyle.single.rawValue]
)
yourLabel.attributedText = attributedText
使用下面的代码
NSString* strPrice = @"£399.95";
NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:strPrice];
[finalString addAttribute: NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger: NSUnderlineStyleSingle] range: NSMakeRange(0, [titleString length])];
self.lblOldPrice.attributedText = finalString;
将文本属性更改为属性并选择文本并右键单击以获取字体属性。单击删除线。
斯威夫特 4 和 5
extension NSAttributedString {
/// Returns a new instance of NSAttributedString with same contents and attributes with strike through added.
/// - Parameter style: value for style you wish to assign to the text.
/// - Returns: a new instance of NSAttributedString with given strike through.
func withStrikeThrough(_ style: Int = 1) -> NSAttributedString {
let attributedString = NSMutableAttributedString(attributedString: self)
attributedString.addAttribute(.strikethroughStyle,
value: style,
range: NSRange(location: 0, length: string.count))
return NSAttributedString(attributedString: attributedString)
}
}
例子
let example = NSAttributedString(string: "440").withStrikeThrough(1)
myLabel.attributedText = example
结果
在 iOS 10.3 上,通过将 NSBaselineOffsetAttributeName 添加到属性字符串中来呈现删除线修复存在问题,如此处所述,带回删除线。覆盖 drawText:in: 可能会很慢,尤其是在 Collection View 或 Table View Cells 上。
一个溶胶 - 添加视图以渲染线条。
第二个解决方案 -
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
attributeString.addAttribute(NSAttributedString.Key.baselineOffset, value: 2, range: NSMakeRange(0, attributeString.length))```
对于那些面临多行文字罢工问题的人
let attributedString = NSMutableAttributedString(string: item.name!)
//necessary if UILabel text is multilines
attributedString.addAttribute(NSBaselineOffsetAttributeName, value: 0, range: NSMakeRange(0, attributedString.length))
attributedString.addAttribute(NSStrikethroughStyleAttributeName, value: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue), range: NSMakeRange(0, attributedString.length))
attributedString.addAttribute(NSStrikethroughColorAttributeName, value: UIColor.darkGray, range: NSMakeRange(0, attributedString.length))
cell.lblName.attributedText = attributedString
创建字符串扩展并添加以下方法
static func makeSlashText(_ text:String) -> NSAttributedString {
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: text)
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
return attributeString
}
然后像这样使用你的标签
yourLabel.attributedText = String.makeSlashText("Hello World!")
这是您可以在 Swift 4 中使用的,因为 NSStrikethroughStyleAttributeName 已更改为 NSAttributedStringKey.strikethroughStyle
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
self.lbl.attributedText = attributeString
Swift5 UILabel 扩展。删除删除线有时不起作用。在这种情况下使用此代码。
extension UILabel {
func strikeThrough(_ isStrikeThrough: Bool = true) {
guard let text = self.text else {
return
}
if isStrikeThrough {
let attributeString = NSMutableAttributedString(string: text)
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0,attributeString.length))
self.attributedText = attributeString
} else {
let attributeString = NSMutableAttributedString(string: text)
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: [], range: NSMakeRange(0,attributeString.length))
self.attributedText = attributeString
}
}
}