ios - 表情符号破坏了我在 UItextView Swift 4 中的代码

标签 ios swift uitextview

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {

    if range.length + range.location > commentView.text!.count{
        return false
    }

    let newLength = (commentView.text?.count)! + text.count - range.length
    let i = charCount - newLength

    if i < 30 {
        charCountLabel.textColor = UIColor.red
    } else {
        charCountLabel.textColor = UIColor(r: 79, g: 79, b: 79)
    }

    charCountLabel.text = "\(i)"
    return newLength < charCount
}

上面的代码是 UITextView 的字符计数器,但是当我在 UITextView 中输入单个表情符号时,编辑就会停止,这是为什么?以及我将如何集成修复

评论 View :UItextView 字符计数:整数 charCountLabel:UIlabel

sc of the debugger

在逐步执行线程时,当我尝试发送另一个字符时,我得到了这个:

further in thread

编辑

在通过调试器时,我发现第二个表情符号或任何字符导致“I”变量成为与“newLength”相同的超长数字......有人有任何想法吗?

最佳答案

我尝试在测试项目中运行您的代码,但遇到了几个问题。我假设您以 0 开始初始化“charCount”,但这会导致当您键入第一个字符时“i”为 -1,然后为之后的每个字符返回 false。

如果您只是想实现文本长度计数器,则有更简单的方法可以实现。添加/删除常规文本和表情符号字符时,以下两种方法会在计数器标签中填充正确的字符计数。

我尝试的第一个方法是实现 textView delegate func textViewDidChange(_ textView: UITextView)。这将在您键入每个字符后更新标签计数。如果需要,您还可以在此处设置文本颜色。

func textViewDidChange(_ textView: UITextView) {
    // only want to update character count label for commentView
    guard textView == commentView, let string = textView.text else {
        return
    }
    // update counter label text with the current text count of the textview
    charCountLabel.text = "\(string.count)"
}

第二种方法是使用您正在使用的 textView 委托(delegate)。这是我在测试项目中使用的一些代码。可能有比这更好的方法,但这会让你继续前进。

@IBOutlet weak var commentView: UITextView!
@IBOutlet weak var charCountLabel: UILabel!

let minCount = 30
let maxCount = 120

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {

    // only want to update character count label for commentView
    guard textView == commentView, let string = textView.text else {
        return true
    }

    // get current text + new character being entered
    var newStr = string+text

    // check if this is a backspace
    let isDeleting = (range.length > 0) && text.isEmpty
    if isDeleting == true {
        // trim last character
        // you may want to drop based on the range.length, however you'll need
        // to determine if the character is an emoji and adjust the number of characters
        // as range.length returns a length > 1 for emojis
        newStr = String(newStr.dropLast())
    }

    // set text color based on whether we're over the min count
    charCountLabel.textColor = newStr.count < minCount ? .red : .blue
    // set the character count in the counter label
    charCountLabel.text = "\(newStr.count)"

    // if we're less than the max count allowed, return true
    return newStr.count < maxCount
}

关于ios - 表情符号破坏了我在 UItextView Swift 4 中的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48290780/

相关文章:

ios - iPhone 不断发送 apple-site-association 请求

swift - 重铸为 NSSplitViewItem 的内容

iphone - 为 UITextView 添加轮廓/描边效果 ios <7

ios - 如何以编程方式导航到 ViewController 并传递数据

iphone - 如何知道ios通讯录是否可以访问Facebook联系人

swift - 端口扬声器的未知选定数据源(类型 : Speaker)?

没有类型的 Swift 类型别名

ios - UITextView setContentOffset :CGPointZero in layoutSubviews doesn't work in iOS8

ios - 将图像粘贴到 UITextView 导致崩溃

等效的 iOS StackPanel