UITextView和UITextField常见的方法差不多。
如下的一个简单例子:
#import "ViewController.h"
@interface ViewController ()<UITextViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITextView *textview=[[UITextView alloc]initWithFrame:CGRectMake(20, 20, 200, 200)];
//textview.backgroundColor=[UIColor grayColor];
//textview.editable=NO;//是否允许编辑
//textview.scrollEnabled=YES;//是否允许滚动
// textview.bounces=NO;//弹簧效果
textview.layer.borderColor=[UIColor redColor].CGColor;//边框效果
textview.layer.borderWidth=1;//边框粗细
textview.layer.cornerRadius=80;//设置圆角
textview.layer.masksToBounds=YES;
textview.delegate = self;
textview.returnKeyType=UIReturnKeyDone;
[self.view addSubview:textview];
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
//检测每次输入的字符
NSLog(@"%@",text);
if ([text isEqualToString:@"
"]) {
[textView resignFirstResponder];
}
return YES;
}