带有文字的UILabel

我想创build一个UILabel ,其中的文字是这样的

在这里输入图像说明

我怎样才能做到这一点? 当文字较小时,线条也应该很小。

SWIFT代码

 let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text") attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length)) 

然后:

 yourLabel.attributedText = attributeString 

为了使一些部分的string罢工,然后提供范围

 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 :指定属性名称的string。 属性键可以由另一个框架提供,也可以是您定义的定制键。 有关在何处查找系统提供的属性键的信息,请参阅NSAttributedString类参考中的概述部分。

value :与名称关联的属性值。

aRange :指定的属性/值对应用的字符范围。

然后

 yourLabel.attributedText = attributeString; 

对于lesser than iOS 6.0 versions您需要使用3-rd party component来执行此操作。 其中一个是TTTAttributedLabel ,另一个是OHAttributedLabel 。

我更喜欢NSAttributedString而不是NSMutableAttributedString这个简单的情况:

 NSAttributedString * title = [[NSAttributedString alloc] initWithString:@"$198" attributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle)}]; [label setAttributedText:title]; 

用于指定属性string的NSUnderlineStyleAttributeNameNSStrikethroughStyleAttributeName属性的常量:

 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代码

 let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text") attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length)) 

然后:

 yourLabel.attributedText = attributeString 

感谢王子的回答 ;)

在Swift中,使用单个删除线样式的枚举:

 let attrString = NSAttributedString(string: "Label Text", attributes: [NSStrikethroughStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue]) label.attributedText = attrString 

其他删除线样式( 请记住使用.rawValue访问枚举 ):

  • NSUnderlineStyle.StyleNone
  • NSUnderlineStyle.StyleSingle
  • NSUnderlineStyle.StyleThick
  • NSUnderlineStyle.StyleDouble

删除线样式(与样式进行或运算):

  • NSUnderlineStyle.PatternDot
  • NSUnderlineStyle.PatternDash
  • NSUnderlineStyle.PatternDashDot
  • NSUnderlineStyle.PatternDashDotDot

指定删除线只能跨单词(而不是空格)应用:

  • NSUnderlineStyle.ByWord

你可以在IOS 6中使用NSMutableAttributedString来完成。

 NSMutableAttributedString *attString=[[NSMutableAttributedString alloc]initWithString:@"$198"]; [attString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(0,[attString length])]; yourLabel.attributedText = attString; 

对于任何人看如何在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 

SWIFT 4

  let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text Goes Here") attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: 0, range: NSMakeRange(0, attributeString.length)) self.lbl_productPrice.attributedText = attributeString 

使用下面的代码

 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; 

对于那些面临多行文字攻击的问题

  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 

在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 

你可以改变你的“strikethroughStyle”像styleSingle,styleThick,styleDouble 在这里输入图像说明

创buildstring扩展并添加下面的方法

 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!")