[swift]向上和向下移动光标

标签: UITextView ios Swift
发布时间: 2016/3/15 21:07:04
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有 UITextView 在我作为用户类型更新的应用程序。但当我开始在 UITextView 中键入时,光标开始闪烁和快速向上和向下移动。如何修复该问题?

这是如何 UITextView 更新-

被称为 Highlighter.swift-可可触摸类文件

import Foundation
import UIKit

// text hightlighter

class SyntaxGroup {

var wordCollection : [String]
var type : String
var color : UIColor

init(wordCollection_I : [String], type_I : String, color_I: UIColor) {

    wordCollection = wordCollection_I
    type = type_I
    color = color_I

}
}

class SyntaxDictionairy {

var collections : [SyntaxGroup] = []

}

class SyntaxRange {

var range : NSRange
var color : UIColor

init (color_I : UIColor, range_I : NSRange) {
    color = color_I
    range = range_I
}

}

class HighLighter {

private var ranges : [SyntaxRange] = []
var highlightedString : NSMutableAttributedString = NSMutableAttributedString()
var syntaxDictionairy : SyntaxDictionairy

init (syntaxDictionairy_I : SyntaxDictionairy) {

    syntaxDictionairy = syntaxDictionairy_I

}

func run(string : String?, completion: (finished: Bool) -> Void) {

    ranges = []
    highlightedString = NSMutableAttributedString()
    var baseString = NSMutableString()

    let qualityOfServiceClass = QOS_CLASS_DEFAULT
    let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
    dispatch_async(backgroundQueue) { () -> Void in

        if string != nil && string != "" {

            self.highlightedString = NSMutableAttributedString(string: string!)

            for i in 0..<self.syntaxDictionairy.collections.count {

                for iB in 0..<self.syntaxDictionairy.collections[i].wordCollection.count {

                    let currentWordToCheck = self.syntaxDictionairy.collections[i].wordCollection[iB]
                    baseString = NSMutableString(string: string!)

                    while baseString.containsString(self.syntaxDictionairy.collections[i].wordCollection[iB]) {

                        let nsRange = (baseString as NSString).rangeOfString(currentWordToCheck)
                        let newSyntaxRange = SyntaxRange(color_I: self.syntaxDictionairy.collections[i].color, range_I: nsRange)
                        self.ranges.append(newSyntaxRange)

                        var replaceString = ""
                        for _ in 0..<nsRange.length {
                            replaceString += "§" // secret unallowed character
                        }
                        baseString.replaceCharactersInRange(nsRange, withString: replaceString)
                    }
                }
            }
            for i in 0..<self.ranges.count {

                self.highlightedString.addAttribute(NSForegroundColorAttributeName, value: self.ranges[i].color, range: self.ranges[i].range)

            }
        }

        dispatch_sync(dispatch_get_main_queue()) { () -> Void in

            completion(finished: true)
        }

    }
}

}

这是在 ViewController.swift 文件-代码

import UIKit

class ViewController: UIViewController, UITextViewDelegate {

@IBOutlet weak var myTextView: UITextView!

var syntaxHighLighter : HighLighter!

override func viewDidLoad() {
    super.viewDidLoad()
    setUpHighLighter()
    myTextView.delegate = self

}

func setUpHighLighter() {

    // build a dict of words to highlight
    let redColor = UIColor(red: 0.5, green: 0.0, blue: 0.0, alpha: 1.0)
    let blueColor = UIColor(red: 0.0, green: 0.0, blue: 0.5, alpha: 1.0)
    let greenColor = UIColor(red: 0.0, green: 0.5, blue: 0.0, alpha: 1.0)

    let redGroup = SyntaxGroup(wordCollection_I: ["red","bordeaux"], type_I: "Color", color_I: redColor)
    let blueGroup = SyntaxGroup(wordCollection_I: ["coralblue","blue","skyblue","azur"], type_I: "Color", color_I: blueColor)
    let greenGroup = SyntaxGroup(wordCollection_I: ["green"], type_I: "Color", color_I: greenColor)

    let dictionairy : SyntaxDictionairy = SyntaxDictionairy()
    dictionairy.collections.append(blueGroup)
    dictionairy.collections.append(greenGroup)
    dictionairy.collections.append(redGroup)

    syntaxHighLighter = HighLighter(syntaxDictionairy_I: dictionairy)

}

func textViewDidChange(textView: UITextView) {
    let currentRange = myTextView.selectedRange
    syntaxHighLighter.run(myTextView.text) { (finished) -> Void in
        self.myTextView.attributedText = self.syntaxHighLighter.highlightedString
        self.myTextView.selectedRange = currentRange
    }

}

   }

解决方法 1:

检查你正在运行最新的操作系统和 Xcode。

试试这个,弄清楚它跳转时 ︰ 您可以再次使用 bool 来检查是否选择的改变是由于重置后突出显示或不。如果它不是您可以使用断点来看看什么。

func textViewDidChangeSelection(textView: UITextView) {

   print("selection changed: \(myTextView.selectedTextRange!)")

}

这可以帮助 ︰ 检查太看到是否 textview 的长度等于仍然突出显示的字符串的长度。

func textViewDidChange(textView: UITextView) {

    let currentRange = myTextView.selectedRange

    syntaxHighLighter.run(myTextView.text) { (finished) -> Void in

        // if the highlighter was slower than typing, ABORT
        guard let textInUITextView = self.myTextfield.attributedText where textInUITextView.length == self.syntaxHighLighter.highlightedString.length else {
            return
        }

        self.myTextView.attributedText = self.syntaxHighLighter.highlightedString
        self.myTextView.selectedRange = currentRange
    }

}

这可以帮助 ︰ 它会阻止运行不止一次的荧光笔。

class HighLighter {

    private var ranges : [SyntaxRange] = []
    var highlightedString : NSMutableAttributedString = NSMutableAttributedString()
    var syntaxDictionairy : SyntaxDictionairy

    private var running : Bool = false

    init (syntaxDictionairy_I : SyntaxDictionairy) {

        syntaxDictionairy = syntaxDictionairy_I

    }

    func run(string : String?, completion: (finished: Bool) -> Void) {

        if running == true {
            print("double action")
            return
        }

        running = true

        ranges = []
        highlightedString = NSMutableAttributedString()
        var baseString = NSMutableString()

        let qualityOfServiceClass = QOS_CLASS_DEFAULT
        let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
        dispatch_async(backgroundQueue) { () -> Void in

            if string != nil && string != "" {

                self.highlightedString = NSMutableAttributedString(string: string!)

                for i in 0..<self.syntaxDictionairy.collections.count {

                    for iB in 0..<self.syntaxDictionairy.collections[i].wordCollection.count {

                        let currentWordToCheck = self.syntaxDictionairy.collections[i].wordCollection[iB]
                        baseString = NSMutableString(string: string!)

                        while baseString.containsString(self.syntaxDictionairy.collections[i].wordCollection[iB]) {

                            let nsRange = (baseString as NSString).rangeOfString(currentWordToCheck)
                            let newSyntaxRange = SyntaxRange(color_I: self.syntaxDictionairy.collections[i].color, range_I: nsRange)
                            self.ranges.append(newSyntaxRange)

                            var replaceString = ""
                            for _ in 0..<nsRange.length {
                                replaceString += "§" // secret unallowed character
                            }
                            baseString.replaceCharactersInRange(nsRange, withString: replaceString)
                        }
                    }
                }
                for i in 0..<self.ranges.count {

                    self.highlightedString.addAttribute(NSForegroundColorAttributeName, value: self.ranges[i].color, range: self.ranges[i].range)

                }
            }

            dispatch_sync(dispatch_get_main_queue()) { () -> Void in
                self.running = false
                completion(finished: true)
            }
        }
    }
}
官方微信
官方QQ群
31647020