UITextView中的焦点模式

时间:2017-04-11 22:33:36

标签: swift string uitextview

你能帮我在iA Writer应用程序上制作像焦点模式这样的东西吗?打字时,除了你写的句子外,一切都很模糊

是否有一种简单的方法可以在UITextView中获得所选范围内的句子范围?像.paragraphRange这样的东西。我尝试使用.lineRange,但它也会返回段落的范围。

let sentenceRange = composeText.lineRange(for: TextView.selectedRange)

如果我想关注段落,它可以正常使用.lineRange或.paragraphRange

    if (focus)
{
   if let stringLength = noteTextView.text?.characters.count
   {
        // Defocus everything
        noteTextView.textStorage.addAttribute(NSForegroundColorAttributeName, value: styles.titleTextColor().withAlphaComponent(0.10), range:NSMakeRange(0, stringLength))

         let sentenceRange = composeText.lineRange(for: noteTextView.selectedRange)

            // Focus 
    noteTextView.textStorage.addAttribute(NSForegroundColorAttributeName, value: styles.titleTextColor().withAlphaComponent(1), range: sentenceRange)

 }

}

但是当我试图找到正确的句子时,它会犯一些错误。

if (focus)
    {
        if let stringLength = noteTextView.text?.characters.count
        {
            // Defocus Everything
            noteTextView.textStorage.addAttribute(NSForegroundColorAttributeName, value: styles.titleTextColor().withAlphaComponent(0.10), range:NSMakeRange(0, stringLength))

            // Find the Sentence to focus

            var arr = [String]()

            noteTextView.text.enumerateSubstrings(in: (noteTextView.text.range(of: noteTextView.text))!, options: NSString.EnumerationOptions.bySentences,
                                                  { (substring, substringRange, enclosingRange, bool) -> () in arr.append(substring!)

            })

            let selectedRange = noteTextView.selectedRange
            var counted : Int = 0
            var focusSentence : String = ""
            var found : Bool = false
            var i : Int = 0

            while(found == false)
            {
                counted += arr[i].characters.count

                if(counted >= selectedRange.location-1)
                {
                    found = true
                    focusSentence = arr[i]
                    counted -= arr[i].characters.count
                }
                else
                {
                    i += 1
                }
            }


            let range = NSMakeRange(counted, focusSentence.characters.count

            noteTextView.textStorage.addAttribute(NSForegroundColorAttributeName, value: styles.titleTextColor().withAlphaComponent(1), range: range)
        }
    }

现在,我正在尝试这个,但是当我改变光标位置时它不起作用。

 if(focus)
    {

        if(x)
        {
            if let stringLength = noteTextView.text?.characters.count
            {
                // Defocus Everything
                noteTextView.textStorage.addAttribute(NSForegroundColorAttributeName, value: styles.titleTextColor().withAlphaComponent(0.1), range:NSMakeRange(0, stringLength))
                x = false
            }
        }

        if(text == "." || text == "!" || text == “?”)
        {
            x = true
        }
        else
        {
            self.noteTextView.typingAttributes = [NSForegroundColorAttributeName : styles.bodyWriterColor().withAlphaComponent(1)]

        }

    }

0 个答案:

没有答案