2017-05-10 36 views
1

我有一个标签:多行属性串与删除线

label.numberOfLines = 0 

而且我试图与此标签删除线的文字:

let index: NSMutableAttributedString = NSMutableAttributedString(string: label.text!) 
index.addAttributes([NSStrikethroughStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue, NSStrikethroughColorAttributeName: UIColor.red], range: NSMakeRange(0, index.length)) 
label.textColor = UIColor.red 
label.attributedText = index 

,这是真的属性串不使用多行或使用numberOfLines设置为0的标签?如果是这样,如何使多行文字删除线?

+0

看看这个链接http://stackoverflow.com/questions/2652163/draw -underlined-strikethrough-text-multiline-string –

+0

是的你是对的。一般来说,它不适用于多行。这是值得看看这个:http://stackoverflow.com/questions/10550732/font-with-strike-through-it –

+0

@SivajeeBattina谢谢。这听起来像是不错的决定,我会试试看,并告诉它是否有效 – kotvaska

回答

0

我想出了两个解决方案。他们基于@SivajeeBattina的答案。

第一个是在http://adamvarga.com/strike/的帮助下删除文本。

private func returnStrikedOutTextFromString(_ str: String) -> String { 
    var newString = "" 

    let normal = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя " 
    let strikethrough = "А̶Б̶В̶Г̶Д̶Е̶Ё̶Ж̶З̶И̶Й̶К̶Л̶М̶Н̶О̶П̶Р̶С̶Т̶У̶Ф̶Х̶Ц̶Ч̶Ш̶Щ̶Ъ̶Ы̶Ь̶Э̶Ю̶Я̶а̶б̶в̶г̶д̶е̶ё̶ж̶з̶и̶й̶к̶л̶м̶н̶о̶п̶р̶с̶т̶у̶ф̶х̶ц̶ч̶ш̶щ̶ъ̶ы̶ь̶э̶ю̶я̶ ̶̶" 

    for i in 0..<str.characters.count { 

     let range: Range<String.Index> = 
       normal.range(of: str 
         .substring(to: str.index(str.startIndex, offsetBy: i + 1)) 
         .substring(from: str.index(str.startIndex, offsetBy: i)))! 
     let index: Int = normal.distance(from: normal.startIndex, to: range.lowerBound) 

     newString = String(format: "%@%@", newString, 
       NSLocalizedString(strikethrough 
         .substring(to: strikethrough.index(strikethrough.startIndex, offsetBy: index + 1)) 
         .substring(from: strikethrough.index(strikethrough.startIndex, offsetBy: index)), 
         comment: "")) 

    } 

    return newString 

} 

而第二个是:https://github.com/GuntisTreulands/UnderLineLabel

1

您的代码应该是这样的,

let index: NSMutableAttributedString = NSMutableAttributedString(string: lbl.text!) 
    index.addAttributes([NSStrikethroughStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue, NSStrikethroughColorAttributeName: UIColor.red], range: NSMakeRange(0, index.length)) 
    lbl.textColor = UIColor.red 
    lbl.attributedText = index 

因为index是你的可变的字符串!不是标题!

而且您不能使用strike throughmulti line标签。

如果你想strike through多行效果,那么你可以使用UITextView而不是标签!

+1

你确定你的答案正在工作,我试着用索引,它不工作。请注意,标签有多行文字。你的回答不能解决这个问题,因为OP想要多行文本罢工 – Krunal

+0

如果它是单行的话,它会起作用! OP中有一个错误,索引是可变字符串不是标题!所以我有正确的! @Krunal – Lion

+0

谢谢你,@Lion。你是对的。这是我的错,因为我有两个不同的标签需要删除线。但我仍然有同样的问题:) – kotvaska

0

写什么样子,

self.label.numberOfLines = 0 
    let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: self.label.text!) 
    attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 1, range: NSMakeRange(0, attributeString.length)) 
    self.label.attributedText = attributeString 

在我结束工作。

enter image description here

+0

它不适用于多行。 – Lion

+0

谢谢,但我已经尝试过了,它不适合我 – kotvaska

+0

请参阅我编辑的答案。它为多行文字工作。 –

6

工作正常多,如果你之前添加NSBaselineOffsetAttributeName:

let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: (object?.title)!) 
attributeString.addAttribute(NSBaselineOffsetAttributeName, value: 0, range: NSMakeRange(0, attributeString.length)) 
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length)) 
+0

你是怎么知道需要'NSBaselineOffsetAttributeName'的? –

+0

意外:-)我在讨论中发现了一个关于tvOS中底层多行字符串的例子。在这个讨论中,一个男人很好奇,为什么每个人都有这个问题时,他没有。他展示了他的代码。唯一的区别在于此基线属性。所以,我尝试了一个删除线多行字符串,并开始工作。 –

+0

什么故事!感谢您找到时间。 –