设置`UITextView`和`UITextField`的最大字符数 [英] Setting maximum number of characters of `UITextView ` and `UITextField `

查看:71
本文介绍了设置`UITextView`和`UITextField`的最大字符数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 UITextView UITextField 。这个数字将以小标签显示(供用户参考,Twitter风格。)

I'd like to set a maximum number of characters allowed to be typed both in a UITextView and a UITextField. This number will be then shown in a little label (for user's reference, Twitter Style.)

推荐答案

试试这个:

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    let newText = (textView.text as NSString).stringByReplacingCharactersInRange(range, withString: text)
    let numberOfChars = newText.characters.count // for Swift use count(newText)
    return numberOfChars < 10;
}

这篇关于设置`UITextView`和`UITextField`的最大字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆