J'ai utilisé cette extension String
pour ajouter strikeThrough au texte
voici l'extension:
if isDone { self.title?.attributedText = title.strikeThrough() } else { self.title?.attributedText = nil self.title?.text = title }
Et voici comment j'utilise pour un UILabel
:
extension String{ func strikeThrough()->NSAttributedString{ let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: self) attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attributeString.length)) return attributeString }
Cela fonctionne bien sous iOS 12, mais sous iOS 13, self.title? .attributedText = nil ne supprime pas la ligne strikeThrough.
Dans mon application, lorsque le strikeThrough est ajouté sur un texte, il sera supprimé de la liste, mais maintenant, il est supprimé, mais strikeThrough apparaîtra sur les autres textes, ce qui ne devrait pas se produire.
Et self.title? n'est pas non plus nul.
Pourriez-vous me suggérer un moyen de supprimer strikeThrough
du texte, le formulaire actuel avec nil
ne fonctionne pas.
Merci beaucoup
4 Réponses :
Au lieu d'utiliser une extension, vous pouvez définir strikeThrough
dans votre contrôleur.
Par exemple:
let strokeEffect: [NSAttributedString.Key : Any] = [ NSAttributedString.Key.strikethroughStyle: NSUnderlineStyle.single.rawValue, NSAttributedString.Key.strikethroughColor: UIColor.gray] self.title?.attributedText = NSAttributedString(string: title, attributes: strokeEffect)
Puis pour supprimer strikeThrough
, vous pouvez simplement changer sa couleur en clear ...
J'ai le même problème sur iOS13, j'utilise attrbutedText dans tableviewCell comme ceci:
- (NSMutableAttributedString *)getNoStrikethroughAttribute:(NSString *)str andTextColor:(UIColor *)textColor{ NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:str ]; [attributeString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:0] range:NSMakeRange(0, [attributeString length])]; if (textColor) { [attributeString addAttribute:NSForegroundColorAttributeName value:textColor range:NSMakeRange(0, [attributeString length])]; } return attributeString; } if (model.finished) { NSMutableAttributedString *mutableAttriStr = [Self getStrikethroughAttribute:text andTextColor:UIColorFromRGB(0x999999)]; cell.leftTitleLbl.attributedText = mutableAttriStr; }else { NSMutableAttributedString *mutableAttriStr = [Self getNoStrikethroughAttribute:text andTextColor:UIColorFromRGB(0x000000)]; cell.leftTitleLbl.attributedText = mutableAttriStr; }
Tout est OK sous la version iOS13.
Mais maintenant sur iOS13, si j'ai vérifié la liste terminée, la cellule sera affichée avec StrikethroughStyle, mais ensuite je suis passée à la liste Non terminé, cellule également affichée avec StrikethroughStyle.
Pour cela, j'essaye de créer deux types d'indentificateurs pour la cellule, et cela fonctionne normalement.
UITableviewCell *cell = nil; if(model.finished){ cell = [tableView dequeueReusableCellWithIdentifier:@"finishedCell"]; }else { cell = [tableView dequeueReusableCellWithIdentifier:@"notFinishedCell"]; } if (cell == nil) { cell = [[ETMTodoFlowCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: model.finished ? @"finishedCell": @"notFinishedCell" ]; } ...
Une autre façon plus élégante de résoudre ce problème:
if(model.finished){ NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString: @"finished_titleString..." ]; [attributeString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(0, [attributeString length])]; cell.leftTitleLabel.attributedText = attributeString; }else { NSMutableAttributedString *mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"notfinished_titleString..."]; cell.leftTitleLbl.attributedText = mutableAttriStr; }
Vous avez exactement le même problème dans iOS 13 lorsque vous essayez de supprimer le style barré de certaines des cellules de vue de tableau (réutilisées). Le barré est là même lorsque vous affectez une nouvelle chaîne AttributedString sans styles. Et vous pouvez voir de debugPrint qu'il n'y a pas du tout de barré dans le style ... Enfin, il faut définir la couleur du trait comme claire. // L'approche ci-dessus consistant à utiliser 2 identificateurs est également bonne.
Je l'ai fait fonctionner en définissant la valeur sur 0 dans le cas où je ne veux plus l'afficher.
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: weightString) //HACK we have to set strikethrough to 0 for ios 13 or it won't remove the strike even if setting the attributed text to nil attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 0, range: NSMakeRange(0, attributeString.length)) cell.weightLabel.attributedText = attributeString))
Sur Swift 4, je l'ai réinitialisé à NSUnderlineStyle.styleNone
pour supprimer le style pour iOS 13. Cependant, styleNone a été supprimé dans comme Swift 4.2 et le mettre à 0 semble obtenir le même effet.
J'ai ajouté ceci à mon extension de chaîne et cela fonctionne. N'oubliez pas que le mode sombre est utilisé:
func strikeThrough() -> NSAttributedString { let attributeString = NSMutableAttributedString(string: self) attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attributeString.length)) attributeString.addAttribute(NSAttributedString.Key.strikethroughColor, value: UIColor.black, range: NSMakeRange(0, attributeString.length)) return attributeString } func noStrikeThrough() -> NSAttributedString { let attributeString = NSMutableAttributedString(string: self) attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attributeString.length)) attributeString.addAttribute(NSAttributedString.Key.strikethroughColor, value: UIColor.clear, range: NSMakeRange(0, attributeString.length)) return attributeString }
Définissez «ne fonctionne pas». Modifiez votre question et indiquez clairement ce qui se passe et ce qui devrait se passer. Et veuillez confirmer que
self.title
n'est pasnil
lorsque ce code est en cours d'exécution.Merci beaucoup, j'ai édité mon premier message.
Qu'est-ce que
title
? UILabel? UITextField? UITextView? Et quelle est la propriététextType
? Votre code manque beaucoup de contexte. Vous devez vraiment publier quelque chose qui reproduit correctement le problème.C'est UILabel, désolé pour le manque d'informations, j'ai environ 1020 lignes de code dans ce contrôleur. TextType concerne la couleur du texte, je le supprime du premier message pour éviter toute confusion
"mais maintenant, il est supprimé mais strikeThrough apparaîtra sur les autres textes" Désolé, mais on ne sait pas quel est le problème. Le code que vous avez montré fonctionne correctement sur iOS 13 pour ajouter ou supprimer des barrés sur un UILabel. Donc, tout ce qui ne va pas, c'est ailleurs.
De plus, on ne sait pas ce qu'est
self
ni où ce code s'exécute. Il ne peut certainement pas être un contrôleur de vue, car un contrôleur de vue a déjà une propriététitle
.Je sais ce que vous voulez dire, puisque j'ai le même problème sur iOS13. Trouvez-vous un moyen de le résoudre?