如何检测Siri是否在iOS的textview中编写文本?

问题描述:

我已将文字视图的限制设置为不允许超过50个字符。如果用户从键盘写入,则不允许添加超过50个字符。但是如果我从声音中添加文字,它会超出限制和突破界限。如何检测Siri是否在iOS的textview中编写文本?

所以我不知道为什么它发生了......

任何人都可以帮我吗?

+1

请告诉你如何把限制字符 –

+0

你应该使用文本字段委托来处理这个。 –

使用该委托,然后再试一次:

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { 
    let strText = (textView.text as NSString).stringByReplacingCharactersInRange(range, withString: text) 
    let numberOfChars = strText.characters.count 
    return numberOfChars < 50; 
} 
+0

我试过这个,但它不工作,这个方法不叫...谢谢你的回复... – aayushi

+0

在你的VC类中添加UITextViewDelegates然后在ViewDidLoad中写入textView.delegate = self –

+0

我已经做了这个只有bt不工作 – aayushi

耶得到了解决的支持

if (textView.text.length > 80) { 
    NSRange range = NSMakeRange(80, textView.text.length - 80); 
    textView.text = [textView.text stringByReplacingCharactersInRange:range withString:@""]; 
} 

感谢

+0

谢谢answer.i我也在寻找这个问题的答案 –

+1

为什么你提供一个Objective-C解决方案,你的问题是用[swift]标记的? –

+1

你会在哪里应用这个条件。 ?? –