UITextView限制编辑

时间:2014-08-19 08:45:17

标签: ios objective-c uibutton uitextview

我有textview给用户可以写一些文字,点击提交按钮后我想限制用户编辑上一个/旧文本。但他们可以添加更多文字。我怎么能这样做?

tips_bgText = [[UITextView alloc]initWithFrame: CGRectMake(610,390,318,231)];

 [self.view addSubview: tips_bgText];

 -(void)submit:(id)sender{

 }

2 个答案:

答案 0 :(得分:1)

请尝试使用这个。请根据你做一些改变。

    .h file
    Declare this variable


    int lastTextCount;

    .m file

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.

        lastTextCount = [tips_bgText.text length];
    }

-(void)submit:(id)sender
{
    lastTextCount = [tips_bgText.text length];
}


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

        if (length >= lastTextCount)
        {
            return YES;
        }
        else
            return NO;
    }

i Hope it helps you

答案 1 :(得分:0)

请尝试以下代码。

@interface ViewController (){
NSString *savedString;
NSString *editString;
}

@end

@implementation ViewController


- (void)viewDidLoad
{
[super viewDidLoad];
savedString  =   @"Hi Hello";
editString  =   @"";

}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range    replacementText:(NSString *)text{
if([text isEqualToString:@""]){
    if(savedString.length != editString.length){
        editString     =   [editString substringToIndex:[editString length] - 1];
        return TRUE;
    }
}else{
    if(editString.length == 0){
        textView.text   =   [savedString stringByAppendingString:text];
    }else
        textView.text   =   [editString stringByAppendingString:text];

    editString      =   textView.text;
    return FALSE;
}
return FALSE;

}

savedString是您点击“提交”后保存的字符串。 希望这可以帮助。

相关问题