2017-05-10 8 views
1

biffés J'ai une étiquette avec:multiligne attribué chaîne avec

label.numberOfLines = 0 

Et je suis en train de faire du texte de cette étiquette avec biffés:

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 

Est-il vrai que la chaîne est attribué pas travailler avec multilines ou avec des étiquettes avec numberOfLines mis à 0? Et si oui, comment faire barrer le texte multiligne?

+0

Regardez ce lien http://stackoverflow.com/questions/2652163/draw -underlined-strikethrough-text-multiline-string –

+0

Oui, vous avez raison. Généralement cela ne fonctionnera pas avec multiline. Il est intéressant de regarder cela: http://stackoverflow.com/questions/10550732/font-with-strike-through-it –

+0

@SivajeeBattina merci. Cela ne semble pas être une mauvaise décision, je vais essayer et dire si cela fonctionne – kotvaska

Répondre

0

je suis venu avec deux solutions. Ils sont basés sur la réponse @SivajeeBattina.

Le premier est de rayer le texte à l'aide de 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 

} 

Et deuxième est: https://github.com/GuntisTreulands/UnderLineLabel

1

Votre code devrait être,

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 

parce index est votre chaîne mutable! pas de titre! Vous ne pouvez pas utiliser strike through avec l'étiquette multi line.

Si vous voulez strike through effet dans plusieurs lignes alors vous pouvez utiliser UITextView au lieu de l'étiquette!

+1

Etes-vous sûr que votre réponse fonctionne, j'ai essayé avec index et ça ne fonctionne pas. Notez que l'étiquette a un texte multiligne. Votre réponse ne peut pas apporter de solution à cette question car OP veut frapper sur le texte multiligne – Krunal

+0

Cela fonctionnera si c'est une ligne simple! Il y a une erreur dans OP, l'index est mutable string not title! donc j'ai correct ça! @Krunal – Lion

+0

merci, @Lion. tu as raison. c'est ma faute de frappe, parce que j'ai deux étiquettes différentes qui doivent être barrées. mais j'ai toujours la même question :) – kotvaska

0

écrire comme,

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 

Travailler à ma fin.

enter image description here

+0

Cela ne fonctionnera pas pour les lignes multiples. – Lion

+0

merci, mais je l'ai essayé et ça ne marche pas pour moi – kotvaska

+0

Voir ma réponse éditée. Son travail pour le texte multiligne. –

6

fonctionne très bien avec multiligne si vous ajoutez NSBaselineOffsetAttributeName avant:

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

Comment avez-vous découvert que NSBaselineOffsetAttributeName est nécessaire? –

+0

Accidentellement :-) J'ai trouvé un exemple dans une discussion, concernant la chaîne multiligne sous-jacente dans tvOS. Dans cette discussion un gars était curieux, pourquoi tout le monde avait ce problème quand il ne l'avait pas fait. Et il a montré son code.La seule différence était dans cet attribut Baseline. Donc, j'ai essayé ceci sur une chaîne multiligne barrée et cela a commencé à fonctionner. –

+0

Quelle histoire! Merci d'avoir trouvé le temps. –