ios - 光标上下移动

标签 ios swift xcode6 cursor uitextview

我的应用程序中有一个 UITextView,它会随着用户输入而更新。但是当我开始在 UITextView 中输入时,光标开始闪烁并快速上下移动。我该如何修复这个错误?

这就是 UITextView 的更新方式-

一个名为 Highlighter.swift- 的 Cocoa Touch 类文件

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
    }

}

   }

最佳答案

检查您是否正在运行最新的操作系统和 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)
            }
        }
    }
}

关于ios - 光标上下移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32412175/

相关文章:

xcode - 从 Xcode 5 升级到 Xcode 6.0.1 后 XCTest.framework 和 MyAppTests.xctest 显示为红色

ios - 下载图像无法在 IOS 设备上运行 - React Native

ios - Xcode 5不显示方案选项以在iOS 6上运行模拟器

android - 具有 H264 解码功能的 WebRTC 视频 Android 和 iOS 客户端

ios - 如何在编辑代码文件时查看 Storyboard 发生的变化,反之亦然?

ios - 如何创建具有符合通用协议(protocol)的弱委托(delegate)的基类?

swift - 基本 : Connecting multiple (View-)Controllers the right way

ios - 后台模式在iOS 8中不起作用

ios - 在 Xcode 中激活自定义字体的目标成员身份

swift - 在 swift 中选择自定义 UICollectionViewCell 时,不会调用 UIView 类中的 drawRect