UITextView

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(100, 50, 200, 150)];
    textView.tag = 10;
    textView.backgroundColor = [UIColor whiteColor];
    textView.text = @"welcome to my city!welcome to my city!welcome to my city!welcome to my city!welcome to my city!welcome to my city!welcome to my city!welcome to my city!welcome to my city!welcome to my city!welcome to my city!";
    textView.textColor = [UIColor blackColor];
    textView.font = [UIFont fontWithName:@"Arial" size:15];
//    textView.secureTextEntry = YES;
    textView.delegate = self;
    textView.returnKeyType = UIReturnKeyDone; //返回键类型
    textView.keyboardType = UIKeyboardTypeDefault;  //键盘类型
    textView.scrollEnabled = YES; //是否可以拖动
    textView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    
    [self.view addSubview:textView];
    [textView release];
}
//按回车键盘消失
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text; {
    
    if ([@"\n" isEqualToString:text] == YES) {
        UITextView *tView = (UITextView*)[self.view viewWithTag:10];
        [tView resignFirstResponder];
        return NO;
    }
    return YES;
}
/按非键盘位置的其它任意位置让键盘消失

//该方法需要先把XIB里的Objects下的UIView类名改为UIControl,然后右键点击Objects下的Control,把鼠标放开事件连线到File`s Owner

- (IBAction)backgroundTap:(id)sender {
    UITextView *tView = (UITextView*)[self.view viewWithTag:10];
    [tView resignFirstResponder];
}

你可能感兴趣的:(UITextView)