2017-08-20 7 views
1

私の最初の質問は、textViewの単語の例 "test"のフォントを変更する方法で、@ bkrlと@Torongoによって正しく答えられました。TextView内のString配列のスタイルを変更する - スウィフト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 
} 

私はまだ上記のコードに慣れていないよ、私はそれが文字列の配列と、1つの文字列だけでなく、ために働くことができるようにコードを一般化するために数行を追加しようとしました。 しかし、それは作品は、それが最終的な変更は、「使用」である最後の言葉のためになり、合理的な原因であるだけで最後の単語のフォントを変更して引き起こさなかったことを確認のために:どのように誰かが助言することができ

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

@Toromgoが提供するコードを改善して、ただ1つの文字列ではなく文字列の配列を処理しますか?

+0

申し訳ありませんが、[AttributedStrings](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/AttributedStrings/AttributedStrings.html)を使用する必要があります –

答えて

2

あなたの質問を変更したので、私はそれに応じて私の答えを更新しました。これを試してください:

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 
    } 

} 

私はコードを実行しています。

+0

"example"という単語を検索すると、textviewは空になります。もし私がStringに何も置かなければ、テキストが見えるでしょう。あなたはそれを試してみていただけますか? –

+0

@Xin Lok申し訳ありません...私は答えを更新しました – Torongo

+0

素晴らしい、それは1つの文字列のためにうまく動作します。私は[string]の配列を1つの文字列の代わりにしようとしました。例えば、[example、test、maybe]の各文字列を太字にするなど、少し難しかったです。文字列の配列のためにこのコードを一般化できますか? –

3

あなたのような拡張子を作成することができます。

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 
    } 

} 

使用法:

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

希望に役立ちます!

+0

あなたを中断して申し訳ありませんが、 、フォントの色ではありません。あなたはちょうどフォントにNSAttributeを変更して太字に変更する必要があります:)とにかく素晴らしい解決策 –

+0

@DominikBucherあなたは正しいです、答えが更新されました。申し訳ありませんが、私の悪い。 – krlb

+0

私が望むように動作します。ありがとう男 –

関連する問題