标签:
创建UITextView
//创建一个单例对象 存储_str字符串 NSUserDefaults * hd = [NSUserDefaults standardUserDefaults]; _str = [hd objectForKey:@"str"]; UITextView * textView = [[UITextView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)]; textView.delegate = self; textView.tag = 1; textView.text = _str; textView.textColor = [UIColor redColor]; textView.textAlignment = NSTextAlignmentCenter; textView.font = [UIFont systemFontOfSize:20]; textView.backgroundColor = [UIColor grayColor]; //设置边框 textView.layer.borderWidth = 1; //设置禁止编辑 // textView.editable = NO; [self.view addSubview:textView];
实现UITextViewDelegate代理方法
#pragma mark - UITextViewDelegate //将要开始编辑 - (BOOL)textViewShouldBeginEditing:(UITextView *)textView{ return YES; } //将要结束编辑 - (BOOL)textViewShouldEndEditing:(UITextView *)textView{ //返回yes 可以退出编辑 返回NO 不可以退出编辑 return YES; } //已经开始编辑 - (void)textViewDidBeginEditing:(UITextView *)textView{ NSLog(@"已经开始编辑"); } //已经结束编辑 - (void)textViewDidEndEditing:(UITextView *)textView{ //可在此存储编辑的文字 NSLog(@"已经结束编辑"); _str = textView.text; NSUserDefaults * hd = [NSUserDefaults standardUserDefaults]; [hd setObject:_str forKey:@"str"]; //写入磁盘里 沙盒目录下 [hd synchronize]; }
取消UITextView的第一响应者
//点击屏幕触发此方法 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //取消textView的第一响应者 UITextView * tv = [self.view viewWithTag:1]; [tv resignFirstResponder]; // [tv becomeFirstResponder]; }
标签:
原文地址:http://www.cnblogs.com/gwkiOS/p/4990255.html