限制UITextView或UITextField中的粘贴字符串长度 [英] Limiting pasted string length in UITextView or UITextField

查看:137
本文介绍了限制UITextView或UITextField中的粘贴字符串长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限制直接输入UITextView或UITextField的字符串的问题已在SO上解决:

The problem of limiting strings that are directly entered into a UITextView or UITextField has been addressed on SO before:

  • iPhone SDK: Set Max Character length TextField
  • iPhone sdk 3.0 issue

然而现在使用OS 3.0复制粘贴成为一个问题,因为上述SO问题中的解决方案不会阻止粘贴其他字符(即,您不能输入超过10个字符使用上述解决方案配置的字段,但您可以轻松地将100个字符粘贴到同一字段中。)

However now with OS 3.0 copy-and-paste becomes an issue, as the solutions in the above SO questions don’t prevent pasting additional characters (i.e. you cannot type more than 10 characters into a field that is configured with the above solutions but you can easily paste 100 characters into the same field).

是否有一种方法可以防止直接输入字符串粘贴字符串溢出?

Is there a means of preventing directly entered string and pasted string overflow?

推荐答案

我能够通过符合te来限制输入和粘贴的文本xtViewDidChange:UITextViewDelegate协议中的方法。

I was able to restrict entered and pasted text by conforming to the textViewDidChange: method within the UITextViewDelegate protocol.

- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length >= 10)
    {
        textView.text = [textView.text substringToIndex:10];
    }
}

但我仍然认为这是一种丑陋的黑客,似乎Apple应该提供UITextFields和UITextViews的某种maxLength属性。

But I still consider this kind of an ugly hack, and it seems Apple should have provided some kind of "maxLength" property of UITextFields and UITextViews.

如果有人知道更好的解决方案,请告诉你。

If anyone is aware of a better solution, please do tell.

这篇关于限制UITextView或UITextField中的粘贴字符串长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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