【问题标题】:How to set a style for a specific word inside UITextView?如何为 UITextView 中的特定单词设置样式?
【发布时间】:2019-12-04 18:49:06
【问题描述】:

我有一个 UITextView,我试图在其中设置特定单词的样式。我面临的问题是,在为单词设置样式时,它还将样式应用于单词的所有其他出现。我只希望单词 say first 或 third 的一个特定实例具有自定义样式。

考虑 UITextView 中存在的文本。

Sunset is the time of day when our sky meets the outer space solar winds. 
There are blue, pink, and purple swirls, spinning and twisting, like clouds of balloons caught in
a whirlwind. The sun moves slowly to hide behind the line of horizon, while the 
moon races to take its place in prominence atop the night sky. People slow to a crawl, 
entranced, fully forgetting the deeds that must still be done. There is a coolness, a 
calmness, when the sun does set.

如果我将样式设置为 sun,那么两个单词的出现都会应用样式。

这里是代码

let normalAttr = [NSAttributedString.Key.font: UIFont(name: "Oswald", size: 19.0), NSAttributedString.Key.paragraphStyle : style]
let customAttr = [NSAttributedString.Key.font: UIFont(name: "Oswald", size: 19.0), NSAttributedString.Key.foregroundColor: UIColor.red]
let words = textView.text.components(separatedBy: " ")
let newText = NSMutableAttributedString()
for word in words {
   if (word == selectedWord) {
     newText.append(NSMutableAttributedString(string: word + " " , attributes: selectedAttributes as [NSAttributedString.Key : Any]))
   } else {
     newText.append(NSMutableAttributedString(string:word + " ", attributes: normalAttributes as [NSAttributedString.Key : Any]))
   }
 }
textView.attributedText = newText

我只想将样式应用于一个单词,有什么帮助吗?

【问题讨论】:

    标签: ios swift string xcode uitextview


    【解决方案1】:

    您如何选择要替换的实例?

    最简单的方法是维护自己的计数器:

    var counter = 0
    for word in words {
       if (word == selectedWord) {
         counter += 1
          // myTarget being the first or third or whatever
         let attributesToUse = (counter == myTarget) ? selectedAttributes : normalAttributes
         newText.append(NSMutableAttributedString(string: word + " " , attributes: attributesToUse as [NSAttributedString.Key : Any]))
       } else {
         newText.append(NSMutableAttributedString(string:word + " ", attributes: normalAttributes as [NSAttributedString.Key : Any]))
       }
     }
    

    但是你当然可以通过使用 NSAttributedStrings 并寻找你的文本范围来变得更干净..

    let myText = NSMutableAttributedString(string: textView.text, attributes: normalAttributes)
    
    // this will only turn up the FIRST occurrence
    if let range = myText.range(of: selectedWord) {
        let rangeOfSelected = NSRange(range, in: myText)
        myText.setAttributes(selectedAttributes, range: rangeOfSelected)
    }
    

    如果你想使用任意出现,你可以编写一个扩展来创建一个包含所有范围的数组,然后选择一个重要的,这是一个很好的参考:https://medium.com/@weijentu/find-and-return-the-ranges-of-all-the-occurrences-of-a-given-string-in-swift-2a2015907a0e

    虽然 Def 可能有点过头了,但您也可以修改那些文章中的方法,改为采用 int (occuranceNumber) 并使用上面的计数器仅返回第 n 次出现的范围,然后执行与属性字符串相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-16
      • 2013-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多