2017-05-11 3 views
1

Je travaille dans Swift avec des chaînes attribuées. Ma question est comment ajouter la chaîne attribuée au texte attribué de UITextField et garder toutes ses couleurs et attributs précédents. Quand j'ajoute une nouvelle chaîne attribuée, tout le texte précédent dans UITextField devient noir et je ne veux pas ça, je veux garder les parties colorées précédentes. C'est le code jusqu'à présent:Ajouter une chaîne attribuée au texte attribué de UITextField et conserver toutes ses couleurs et attributs précédents

var newMutableString = NSMutableAttributedString() 

if let completionTextHolderText = completionTextHolderText { 
    newMutableString = userTextField.attributedText?.mutableCopy() as! NSMutableAttributedString 
    let choosenSuggestion = createAttributed(string: completionTextHolderText, with: .blue) 
    newMutableString.append(choosenSuggestion) 

    let emptySpace = createAttributed(string: " ", with: .black) 
    newMutableString.append(emptySpace) 
} 

userTextField.attributedText = newMutableString.copy() as? NSAttributedString 

//------- 
    private func createAttributed(string: String, with color: UIColor) -> NSMutableAttributedString { 
      let attributedString = NSMutableAttributedString(string: string, attributes: [:]) 
      attributedString.addAttribute(NSForegroundColorAttributeName, 
             value: color, 
             range: NSRange(location: 0, length: string.characters.count)) 
      return attributedString 
    } 

Merci pour votre aide.

+0

manière simple, il devrait fonctionner. Mais quels attributs userTextField.attributedText a quand ce code statrts et que fait la méthode createAttributed()? –

+0

@AlexShubin - J'ai modifié ma question et ajouté la fonction 'createAttributed()'. 'userTextField.attributedText' pourrait contenir du texte bleu ou noir. Seuls les attributs sont 'NSForegroundColorAttributeName'. Par exemple, un mot de couleur noire, un mot de couleur noire, un mot de couleur bleue, un mot de couleur noire, un mot de couleur bleue, etc. L'ordre n'est pas important. –

+0

Votre code fonctionne maintenant. Je pense que quelque chose se passe après votre section principale et réinitialise la couleur d'attribut pour la gamme. S'il vous plaît poster plus de code pour la section principale –

Répondre

0

Votre code fonctionne maintenant. Je pense que quelque chose se produit avant ou après votre section principale et réinitialise la couleur d'attribut pour la gamme.

Et voici une idée de la façon dont vous pouvez refactoriser:

@IBAction func buttonClick(_ sender: UIButton) { 

    if let completionTextHolderText = completionTextHolderText, 
     let attrStr = userTextField.attributedText { 

     let newMutableString = attrStr.mutableCopy() as! NSMutableAttributedString 

     newMutableString.append(
      createAttributed(string: completionTextHolderText+" ", with: .blue) 
     ) 

     userTextField.attributedText = newMutableString 
    } 
} 

private func createAttributed(string: String, with color: UIColor) -> NSAttributedString { 
    return NSAttributedString(string: string, attributes: [NSForegroundColorAttributeName : color]) 
} 
+1

Oui, avant ce code, je change le texte de UITextField. Et c'était un problème. –