UITextView里换行实现(键盘上附加一个完成按钮)

// 1、创建UITextView

 UITextView* tv = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 300, 200)];

[tv setFont:[UIFont systemFontOfSize:19.0]];

tv.tag = 1001;

    tv.returnKeyType = UIReturnKeyNext; // 下一行

tv.delegate = self;

[self.view addSubview:tv];


 // 2、键盘上方附加一个toolbar,toolbar上有个完成按钮

 UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];

keyboardDoneButtonView.barStyle = UIBarStyleDefault;

keyboardDoneButtonView.translucent = YES;

keyboardDoneButtonView.tintColor = nil;

[keyboardDoneButtonView sizeToFit];

 // toolbar上的2个按钮

 UIBarButtonItem *SpaceButton=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; // 让完成按钮显示在右侧

 UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"完成"

 style:UIBarButtonItemStyleBorderedtarget:self

 action:@selector(pickerDoneClicked)];

[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:SpaceButton, doneButton, nil]];

tv.inputAccessoryView = keyboardDoneButtonView;

// 3、键盘上方附加的完成按钮触发函数

-(void)pickerDoneClicked

   {

       UITextView* view = (UITextView*)[self.view viewWithTag:1001];

[view resignFirstResponder];

   }

你可能感兴趣的:(UITextView里换行实现(键盘上附加一个完成按钮))