Problem:
Use uitextview to display multiple lines of text
Create a simple textview:
- (void)viewDidLoad{[super viewDidLoad];self.view.backgroundColor = [UIColor whiteColor];self.myTextView = [[UITextView alloc] initWithFrame:self.view.bounds]; self.myTextView.text = @"Some text here...";self.myTextView.font = [UIFont systemFontOfSize:16.0f];[self.view addSubview:self.myTextView];}
When there are many texts in textview, click Text View, and you will find that a keyboard will pop up from the bottom of the screen, covering almost half of the text view area. This means that if you start to input text and reach the center of the text view, you will not see the text entered later.
To correct this problem, we need to listen for some notifications as follows:
Uikeyboarwillshownotification
The system will send this notification no matter what UI components (such as text field and Text View) Cause keyboard display.
Uikeyboarddidshownotification
The system will send this notification when the keyboard is already displayed on the screen.
Uikeyboardwillhidenotification
The system will send this notification when the keyboard is about to be hidden.
Uikeyboarddidhidenotification
The system will send this notification when the keyboard is completely hidden.
Note: A keyboard notification contains a dictionary. You can use it using the userinfo attribute, which specifies the keyboard's border on the screen. This attribute belongs to the nsdictionary type. In this dictionary, there is a keyword named uikeyboardframeenduserinfokey, which contains an nsvalue-type object that contains a rectangular boundary when the keyboard is fully displayed. This rectangle is marked as a cgrect.
Therefore, our policy is to find out when the keyboard will be displayed on the screen and then adjust the view. To implement this policy, we will use the contentinset attribute of uitextview to specify the Edge Distance of the text content, including the distance to the top, left, bottom, and right of the text view.
Code:
- (void)viewWillAppear:(BOOL)animated{ [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];}- (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [[NSNotificationCenter defaultCenter]removeObserver:self];}
In this Code, in method viewwillappear: Start listening to keyboard notifications and end listening in method viewwilldisappear. When no listener is required, it is very important to remove the listener. The reason behind this is that when the View Controller is no longer displayed, It is not removed as a view control for listening to keyboard notifications, the keydisk notifications issued by components of any other view controls will cause the notification center to send these notifications to this view controller.
- (void)handleKeyboardDidShow:(NSNotification *)paramNitification{ //得到键盘的frame NSValue *keyboardRectAsObject = [[paramNitification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey]; //存入keyboardRect CGRect keyboardRect; [keyboardRectAsObject getValue:&keyboardRect]; //给TextView下边距,使其达到顶端的键盘 _myTextView.contentInset = UIEdgeInsetsMake(0, 0, keyboardRect.size.height, 0);}- (void)handleKeyboardWillHide:(NSNotification *)paramNotification{ //将TextView变回原来大小 _myTextView.contentInset = UIEdgeInsetsZero;}
// Process a series of messages after receiving the notification
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; _myTextView = [[UITextView alloc]initWithFrame:self.view.bounds]; _myTextView.text = @"Some text here..."; _myTextView.font = [UIFont systemFontOfSize:16.0f]; [self.view addSubview:_myTextView];}
Ui: Use uitextview to display multiple lines of text (with keyboard processing)