UILabel文字突破

时间:2012-10-30 05:11:41

标签: ios swift cocoa-touch uilabel strikethrough

我想创建一个UILabel,其中的文字就像这样

enter image description here

我该怎么做?当文本很小时,该行也应该很小。

19 个答案:

答案 0 :(得分:181)

SWIFT CODE

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

然后:

yourLabel.attributedText = attributeString

要使字符串的某些部分出现,请提供范围

let somePartStringRange = (yourStringHere as NSString).range(of: "Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: somePartStringRange)

<强>目标C

iOS 6.0&gt; 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类参考中的概述部分。

:与name关联的属性值。

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

然后

yourLabel.attributedText = attributeString;

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

答案 1 :(得分:35)

对于这个简单的案例,我更喜欢NSAttributedString而不是NSMutableAttributedString

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

用于指定属性字符串的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;  

答案 2 :(得分:22)

SWIFT CODE

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

然后:

yourLabel.attributedText = attributeString

感谢Prince answer;)

答案 3 :(得分:18)

Swift 4.0中的删除线

let attributeString =  NSMutableAttributedString(string: "Your Text")

attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle,
                                 value: NSUnderlineStyle.styleSingle.rawValue,
                                 range: NSMakeRange(0, attributeString.length))
self.yourLabel.attributedText = attributeString

它对我来说就像一个魅力。

将其用作扩展名

extension String {
    func strikeThrough() -> NSAttributedString {
        let attributeString =  NSMutableAttributedString(string: self)
        attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0,attributeString.length))
        return attributeString
    }
}

像这样打电话

myLabel.attributedText = "my string".strikeThrough()

答案 4 :(得分:11)

SWIFT 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

其他方法是使用String 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
    }
}

调用函数:像这样使用它

testUILabel.attributedText = "Your Text Goes Here!".strikeThrough()

归功于@Yahya - 2017年12月更新
归功于@kuzdu - 2018年8月更新

答案 5 :(得分:9)

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

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

答案 6 :(得分:6)

在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

您可以像styleSingle,styleThick,styleDouble一样更改“strikethroughStyle” enter image description here

答案 7 :(得分:3)

对于在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

答案 8 :(得分:2)

我可能迟到了聚会。

无论如何,我知道NSMutableAttributedString,但最近我以略有不同的方法实现了相同的功能。

  • 我添加了高度= 1的UIView。
  • 将UIView的前导和尾随约束与标签的前导和尾随约束
  • 匹配
  • 将UIView对准Label的中心

按照上述所有步骤操作后,我的Label,UIView及其约束看起来如下图所示。

enter image description here

答案 9 :(得分:1)

使用以下代码

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;   

答案 10 :(得分:1)

在iOS 10.3上,通过向属性字符串中添加NSBaselineOffsetAttributeName(如此处所述)来渲染删除线行,在渲染删除线行修复方面存在问题。覆盖drawText:in:可能会很慢,尤其是在“集合视图”或“表格视图”单元格上。

一个sol-添加视图以渲染一条线。

第二溶胶-

attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
attributeString.addAttribute(NSAttributedString.Key.baselineOffset, value: 2, range: NSMakeRange(0, attributeString.length))```

答案 11 :(得分:1)

快速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

结果

enter image description here

答案 12 :(得分:1)

Swift 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

答案 13 :(得分:0)

Swift 5 - 简短版本

let attrString = NSAttributedString(string: "Label Text", attributes: [NSAttributedString.Key.strikethroughStyle: NSUnderlineStyle.single.rawValue])

yourLabel.attributedText = attrString

答案 14 :(得分:0)

快速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()

答案 15 :(得分:0)

将text属性更改为attributed并选择文本,然后单击鼠标右键以获取font属性。单击删除线。 Screenshot

答案 16 :(得分:0)

这是您可以在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

答案 17 :(得分:0)

创建字符串扩展名并添加以下方法

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

答案 18 :(得分:0)

对于那些面临多行文字罢工问题的人

    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