如何为现有的UITextView切换autocorrectionType的开启和关闭 [英] How to toggle autocorrectionType on and off for an existing UITextView

查看:1101
本文介绍了如何为现有的UITextView切换autocorrectionType的开启和关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITextView在我的iPhone应用程序,我想要能够切换autocorrectionType。



当用户编辑文本视图时,我想将autocorrectionType设置为UIAutocorrectionTypeYes。
当文本视图没有被编辑时,我想要将autocorrectionType设置为UIAutocorrectionTypeNo(因为我不希望任何由自动更正生成的红色下划线是可见的)



只需切换autocorrectionType,如下:

  myTextView.autocorrectionType = UITextAutocorrectionTypeYes; 
myTextView.autocorrectionType = UITextAutocorrectionTypeNo;

似乎无效。
当我可以切换autocorrectionType或在哪些情况下更改的autocorrectionType生效时是否有限制?



编辑: p>

说明:
初始化UITextView时没有设置autocorrectionType的问题。当我想要更改现有UITextView的autocorrectionType时,会出现问题。在我的情况下,我想给用户编辑一个UITextView时的自动更正的优势,但不希望任何拼写错误指出与红色虚线下划线当UITextView不被编辑 - 部分是因为我也导出一个UIView包含UITextView作为图像。
问题是,只是更改我的UITextView的autocorrectionType属性的值不工作。

解决方案

这里有一个简单的方法来做这个图像导出场景:

   - (BOOL)textViewShouldBeginEditing:(UITextView *)textView 
{
//翻译拼写检查
textView.autocorrectionType = UITextAutocorrectionTypeYes;
return YES;
}


- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
//关闭拼写检查并清除红色花纹。
textView.autocorrectionType = UITextAutocorrectionTypeNo;
NSString * currentText = textView.text;
textView.text = @;
textView.text = currentText;
return YES;
}


I have a UITextView in my iPhone app for which I want to be able to toggle the autocorrectionType.

When a user is editing the text view, I want the autocorrectionType to be set to UIAutocorrectionTypeYes. When the text view is not being edited, I want the autocorrectionType to be set to UIAutocorrectionTypeNo (because I don't want any of the red-dotted underlines generated by autocorrection to be visible)

Simply toggling the autocorrectionType like this:

myTextView.autocorrectionType = UITextAutocorrectionTypeYes;
myTextView.autocorrectionType = UITextAutocorrectionTypeNo;

Doesn't seem to work. Are there limitations on when I can toggle the autocorrectionType or in which situations the changed autocorrectionType takes effect?

EDIT:

To clarify: There are no issues setting the autocorrectionType when initializing the UITextView. The problem arises when I want to change the autocorrectionType for an existing UITextView. In my case I want to give users the advantage of autocorrection when they edit a UITextView, but don't want any spelling errors pointed out with the red-dotted underlines when the UITextView is not being edited - in part because I am also exporting a UIView containing the UITextView as an image. The problem is that just changing the value of my UITextView's autocorrectionType property doesn't work.

解决方案

Here's an easy way to do this for the image export scenario :

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    // Turn spell check on
    textView.autocorrectionType = UITextAutocorrectionTypeYes;
    return YES;
}


- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    // Turn spell check off and clean up red squiggles.
    textView.autocorrectionType = UITextAutocorrectionTypeNo;
    NSString *currentText = textView.text;
    textView.text = @"";
    textView.text = currentText;
    return YES;
}

这篇关于如何为现有的UITextView切换autocorrectionType的开启和关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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