gpt4 book ai didi

ios - 删除表情符号错误

转载 作者:行者123 更新时间:2023-11-28 08:24:00 30 4
gpt4 key购买 nike

我点击了这个链接 Find out if Character in String is emoji?尝试限制我的文本字段的字符数量,即使使用表情符号也是如此。根据这个链接,我必须根据表情符号的字形来计算表情符号,除了一个小错误外,一切正常,当我的字形计数达到时,它不再让我删除字符串。

这是使用 UITextField Delegate 处理字符限制的代码

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

var canEditString : Bool = false

if textField == self.descTxtField {

// Description Text Field
if textField.text != nil || textField.text != "" {

let currentString = textField.text
if (currentString?.containsEmoji)! {
print("I have an emoji")
let glyphCount = currentString!.glyphCount
print(glyphCount)
canEditString = glyphCount <= descriptionLimit
}else {
print("I have no emoji's")
let currentCharacterCount = textField.text?.characters.count ?? 0
//print(currentCharacterCount)
if (range.length + range.location > currentCharacterCount){
return false
}
let newLength = currentCharacterCount + string.characters.count - range.length
canEditString = newLength <= descriptionLimit
}
}
return canEditString


} else {

// Number Text Field
let currentCharacterCount = textField.text?.characters.count ?? 0
if (range.length + range.location > currentCharacterCount){
return false
}
let newLength = currentCharacterCount + string.characters.count - range.length
return newLength <= numberLimit
}
}

最佳答案

String Manifesto

swift 4

表情符号现在的字符数为 1,这使得跟踪字符串中的字符数变得更加容易。

关于ios - 删除表情符号错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40567138/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com