2017-08-20 15 views
1

Ma première question était de savoir comment changer la police d'un exemple de mot "test" dans un textView, et il a été répondu correctement par @bkrl et @Torongo.Modifier le style d'un tableau de String dans un TextView - swift - EDIT

func changeAllOccurence(of string: String, font: UIFont) -> NSAttributedString { 
    let attributedString = NSMutableAttributedString(string: self) 
    var range = NSMakeRange(0, attributedString.mutableString.length) 

    while(range.location != NSNotFound) 
    { 
     range = attributedString.mutableString.range(of: string, options: .caseInsensitive, range: range) 
     if(range.location != NSNotFound) 
     { 
      attributedString.addAttribute(NSFontAttributeName, 
              value: font, 
              range: range) 
      range = NSMakeRange(range.location + range.length, self.characters.count - (range.location + range.length)); 
     } 
    } 

    return attributedString 
} 

Comme je ne suis toujours pas au courant du code ci-dessus, j'ai essayé d'ajouter quelques lignes afin de généraliser le code afin qu'il puisse fonctionne pour un tableau de chaînes et non une seule chaîne. Mais pour que cela n'a pas été travaux sont la cause a changé la police du dernier mot que ce qui est un motif raisonnable les modifications finales seront le dernier mot qui est « usage »: quelqu'un

 let words = ["example", "usage"] 
     for word in words { 
     let attributedText = text.changeAllOccurence(of: word, font: UIFont.boldSystemFont(ofSize: 17)) 
     textview.attributedText = attributedText 
    } 

peut vous conseiller comment améliorer le code fourni par @Toromgo afin de travailler pour n'importe quel tableau de chaînes au lieu d'un seul?

+0

Désolé, mais vous devez utiliser [AttributedStrings] (https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/AttributedStrings/AttributedStrings.html) –

Répondre

2

Comme vous avez changé votre question, j'ai mis à jour ma réponse en conséquence. S'il vous plaît essayez ceci:

extension String { 

    func changeAllOccurence(of strings: [String], font: UIFont) -> NSAttributedString { 
     let attributedString = NSMutableAttributedString(string: self) 
     for eachString in strings { 
      var range = NSMakeRange(0, attributedString.mutableString.length) 

      while(range.location != NSNotFound) 
      { 
       range = attributedString.mutableString.range(of: eachString, options: .caseInsensitive, range: range) 
       if(range.location != NSNotFound) 
       { 
        attributedString.addAttribute(NSFontAttributeName, 
                value: font, 
                range: range) 
        range = NSMakeRange(range.location + range.length, self.characters.count - (range.location + range.length)); 
       } 
      } 
     } 
     return attributedString 
    } 

} 

J'ai exécuté le code et il fonctionne.

+0

Si je vais chercher un mot "exemple", le textview sera vide. Si je ne mets rien "" dans String, le texte sera visible. Pouvez-vous s'il vous plaît essayer? –

+0

@Xin Lok Désolé ... J'ai mis à jour la réponse – Torongo

+0

Super, ça marche bien pour une corde. J'ai essayé de le faire pour un tableau de [chaîne] au lieu d'une chaîne, par exemple pour mettre en gras chaque chaîne dans [exemple, test, peut-être] mais c'était un peu dur. pouvons-nous généraliser ce code pour travailler pour un tableau de chaîne? –

3

Vous pouvez créer l'extension comme:

extension String { 

    func change(font: UIFont, of string: String) -> NSAttributedString { 
     let attributedString = NSMutableAttributedString(string: self) 
     let subStringRange = attributedString.mutableString.range(of: string, 
                    options: .caseInsensitive) 
     if subStringRange.location != NSNotFound { 
      attributedString.addAttribute(NSFontAttributeName, 
              value: font, 
              range: subStringRange) 
     } 
     return attributedString 
    } 

} 

Utilisation:

let text = "This is example of usage extension" 
let attributedText = text.change(font: UIFont.boldSystemFont(ofSize: 17), of: "example") 
textView.attributedText = attributedText 

Hope that helps!

+0

Désolé de vous interrompre, mais la question concerne la police , pas à propos de la couleur de la police. Vous avez juste besoin de chane le NSAttribute à la police et le changer en gras :) Quoi qu'il en soit belle solution –

+0

@DominikBucher Vous avez raison, répondez à jour. Désolé mon mauvais. – krlb

+0

Cela fonctionne comme je le veux. Merci homme –