0

Je fais une application de prise de notes. J'enregistre Attribué text Sur le détailVC et je montre l'aperçu dans l'étiquette UITableViewCell.swift - comment puis-je supprimer une ligne vierge (vide) dans NSAttributedString?

Dans l'étiquette j'ai un texte comme celui-ci

The Bold Title 

my Text 

et je veux changer cela comme

The Bold Title 
my Text 

J'ai trouvé la solution dans chaîne

let regex = try! NSRegularExpression(pattern: "\n+", options: []) 
let newString = regex.stringByReplacingMatches(in: MyString, options: [], range: NSRange(location: 0, length: MyString.characters.count), withTemplate: "\n") 

mais comment puis-je le faire en NSAttirbutedStr?

pls aider!

Répondre

1

Vous pouvez créer un NSMutableAttributedString qui est une chaîne affectée permettant la mutation de son contenu, puis utiliser la propriété mutableString.

Si vous avez déjà un NSAttributedString, vous pouvez utiliser mutableCopy() ou jeter un oeil à possible initializers of NSMutableAttributedString.

let str = NSMutableAttributedString(string: "Hardcore\n\nHenry") 
str.mutableString.replaceOccurrences(of: "\n\n", with: "\n", options: [], range: NSMakeRange(0, str.length)) 
print(str) 
+0

Merci beaucoup !! mais il n'a aucun attribut! Comment puis-je réparer cela? – Daniel

+0

Vous pouvez ajouter des attributs comme bon vous semble. Par exemple, vous pouvez utiliser 'str.addAttribute (NSForegroundColorAttributeName, valeur: UIColor.red, plage: NSMakeRange (0, 8))' (c'est swift3, pour swift2.x, utilisez 'UIColor.redColor()') pour faire le mot 'Hardcore' rouge. Cela n'apparaîtra pas dans 'print()' tough. Vous devez utiliser un 'UILabel' pour voir à quoi cela va ressembler. – Majster

+0

Merci ... mais mon problème est de remplacer le texte attribué ** en gardant ** ses propres attributs. (J'ai utilisé UILabel ...) – Daniel