欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

限制中文输入长度

程序员文章站 2022-03-02 12:09:36
...
 let toBeString = textView.text
 let lang = textView.textInputMode?.primaryLanguage
 if lang == "zh-Hans" {
   let selectedRange = textView.markedTextRange
   if selectedRange == nil && (toBeString?.characters.count)! > 50{
          let index = toBeString?.index((toBeString?.startIndex)!, offsetBy: 50)
          textView.text = toBeString?.substring(to:index!)
    }
}else if (toBeString?.characters.count)! > 50  {
       let index = toBeString?.index((toBeString?.startIndex)!, offsetBy: 50)
       textView.text = toBeString?.substring(to:index!)
}