3

如何为标签添加删除线样式。UILabel 中数字的中间删除行。检查附加图像以供参考。

在此处输入图像描述

4

5 回答 5

3

使用属性字符串

截屏

在此处输入图像描述

代码

NSDictionary * attribtues = @{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle),
                              NSStrikethroughColorAttributeName:[UIColor redColor]};
NSAttributedString * attr = [[NSAttributedString alloc] initWithString:@"label" attributes:attribtues];
self.label.attributedText = attr;
于 2015-11-20T03:40:10.177 回答
2

来自@krunal Swift4 答案的更新函数

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
    }
}

和调用函数是一样的

yourLabel.attributedText = "yourString".strikeThrough()
于 2020-01-14T15:58:33.947 回答
1

如果想使用 UILabel 扩展

extension UILabel {
  func strikeThroughText() {
    let attributeString =  NSMutableAttributedString(string: self.text ?? "")
    attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0,attributeString.length))
    self.attributedText = attributeString
  }
}
于 2020-12-29T20:06:03.620 回答
1

以编程方式使用以下代码

更喜欢使用扩展:

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

结果看起来像这样。

在此处输入图像描述

于 2018-12-28T12:55:11.070 回答
0

在情节提要中为 UILabel 添加中间删除行

  1. 在情节提要中点击 uilabel,查看实用程序。选择Attributed

    公用事业

  2. 点击字体图标,打开字体面板。

    字体面板

  3. 选择要更改的文本

  4. 选择中间线(例如:Single)

  5. 完全的。

    结果

于 2018-10-04T08:37:08.230 回答