限制用户在UITextView iOS中最多输入50个单词 [英] Restrict the user to enter max 50 words in UITextView iOS

查看:96
本文介绍了限制用户在UITextView iOS中最多输入50个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图限制用户在UITextView中最多输入50个单词.我从 question1 中尝试了PengOne的解决方案.这对我有用,除了用户可以在最后一个单词中输入无限个字符.

I am trying to restrict the user to enter max 50 words in UITextView. I tried solution by PengOne from this question1 . This works for me except user can enter unlimited chars in the last word.

所以我想到了使用正则表达式.我正在使用由给出的正则表达式 VonC在 question2 中.但这不允许我在文本视图中输入特殊符号,例如"@".

So I thought of using regular expression. I am using the regular expression given by VonC in this question2. But this does not allow me enter special symbols like , " @ in the text view.

在我的应用程序中,用户可以在UITextView中输入任何内容,也可以仅从网页,注释,电子邮件等中复制粘贴.

In my app , user can enter anything in the UITextView or just copy-paste from web page , notes , email etc.

有人可以为此找到其他解决方案吗?

Can anybody know any alternate solution for this ?

提前谢谢.

推荐答案

此代码应为您工作.

  - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
    {
        return textView.text.length + (text.length - range.length) <= 50;
    }

这篇关于限制用户在UITextView iOS中最多输入50个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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