Ui: Use uitextview to display multiple lines of text (with keyboard processing)

Source: Internet
Author: User
Tags notification center

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.