Iphone application development 2: A detailed description of the UITextView Control

Source: Internet
Author: User

 

1. Create and initialize

Create a UITextView file and write the following code in the. h file:

 

 

# Import <UIKit/UIKit. h>

 

@ Interface TextViewController: UIViewController <UITextViewDelegate>

{

UITextView * textView;

}

 

@ Property (nonatomic, retain) UITextView * textView;

 

@ End

 

 

 

 

Initialize the textview in the. m file and write the following code:

 

Self. textView = [[[UITextView alloc] initWithFrame: self. view. frame] autorelease]; // initialize the size and release it automatically

 

Self. textView. textColor = [UIColor blackColor]; // you can specify the font color in textview.

 

Self. textView. font = [UIFont fontWithName: @ "Arial" size: 18.0]; // set the font name and font size.

 

Self. textView. delegate = self; // sets its delegate method.

 

Self. textView. backgroundColor = [UIColor whiteColor]; // set the background color.

 

 

Self. textView. text = @ "Now is the time for all good developers tocome to serve their country. \ n \ nNow is the time for all good developers to cometo serve their country. "; // set the content it displays

 

Self. textView. returnKeyType = UIReturnKeyDefault; // type of the Return key

 

Self. textView. keyboardType = UIKeyboardTypeDefault; // keyboard type

 

Self. textView. scrollEnabled = YES; // whether to drag

 

 

Self. textView. autoresizingMask = UIViewAutoresizingFlexibleHeight; // adaptive height

 

 

[Self. view addSubview: self. textView]; // Add to the entire page

 

 

 

 

2. Several Methods for UITextView to exit the keyboard

Because you click UITextView, the keyboard will appear. If you exit the keyboard, there are several ways:

(1) If your program has a navigation bar, you can add a Done button on the navigation bar to exit the keyboard. Of course, you must first implement UITextViewDelegate.

The Code is as follows:

 

-(Void) textViewDidBeginEditing :( UITextView *) textView {

 

UIBarButtonItem * done = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target: self action: @ selector (leaveEditMode)] autorelease];

 

Self. navigationItem. rightBarButtonItem = done;

 

}

 

-(Void) textViewDidEndEditing :( UITextView *) textView {

 

Self. navigationItem. rightBarButtonItem = nil;

 

}

 

-(Void) leaveEditMode {

 

[Self. textView resignFirstResponder];

 

}

 

 

 

(2) If you do not need the Enter key in the textview, you can use the Enter key as the response key to exit the keyboard.

The Code is as follows:

 

# Pragma mark-UITextView Delegate Methods

 

-(BOOL) textView :( UITextView *) textView shouldChangeTextInRange :( nsange) range replacementText :( NSString *) text

 

{

 

If ([text isEqualToString: @ "\ n"]) {

 

[TextView resignFirstResponder];

 

Return NO;

 

}

 

Return YES;

 

}

 

 

 

 

In this way, whether you use the Enter key on the keyboard or the return key in the pop-up keyboard, you can exit the keyboard.

(3) You can also customize other loaded keyboards to exit. For example, you can add a view on the pop-up keyboard to place the Done button for exiting the keyboard.

The Code is as follows:

 

 

UIToolbar * topView = [[UIToolbar alloc] initWithFrame: CGRectMake (0, 0,320, 30)];

 

[TopView setBarStyle: UIBarStyleBlack];

 

UIBarButtonItem * helloButton = [[UIBarButtonItem alloc] initWithTitle: @ "Hello" style: UIBarButtonItemStyleBordered target: self action: nil];

 

UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target: self action: nil];

 

 

UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithTitle: @ "Done" style: UIBarButtonItemStyleDone target: self action: @ selector (dismissKeyBoard)];

 

NSArray * buttonsArray = [NSArray arrayWithObjects: helloButton, btnSpace, doneButton, nil];

 

[DoneButton release];

 

[BtnSpace release];

 

[HelloButton release];

 

[TopView setItems: buttonsArray];

 

[TvTextView setInputAccessoryView: topView];

 

-(IBAction) dismissKeyBoard

 

{

 

[TvTextView resignFirstResponder];

 

}

 

(4) Setting UITextView rounded corners

After # import QuartzCore/QuartzCore. h, you can use [textView. layer setCornerRadius: 10 ];

(5) UITextView adaptive height based on text size

The height is determined by the number of characters in the text as follows:

 

 

NSString * desc = @ "Description it is a test font, and don't become angry for which I use to do here. Now here is a very nice party from american or not! ";

 

CGSize size = [desc sizeWithFont: [UIFont systemFontOfSize: 14] constrainedToSize: CGSizeMake (240,200 0) lineBreakMode: UILineBreakModeWordWrap];

 

 

 

Only numberoflines to be defined by UILabel are 0, that is, the number of rows is not limited. As follows:

 

[Label setNumberOfLines: 0];

[Label setFrame: CGRectMake (40,135,240, size. height + 10)];

[Label setText: desc];

 

 

 

 

 

 

(6) UITextView menu after custom selection of Text

Add the following to ViewDidLoad:

 

UIMenuItem * menuItem = [[UIMenuItem alloc] initWithTitle: @ "share to Sina Weibo" action: @ selector (changeColor :)];

UIMenuController * menu = [UIMenuController sharedMenuController];

[Menu setMenuItems: [NSArray arrayWithObject: menuItem];

[MenuItem release];

 

 

Of course, you can write the changeColor method in the above @ selector, that is, the method that will be triggered after you click the custom menu item.

Then add a method to the Code:

 

-(BOOL) can1_maction :( SEL) action withSender :( id) sender

{

If (action ==@ selector (changeColor :))

{

If (textView. selectedRange. length> 0)

Return YES;

}

Return NO;

}

 

 

 

After implementation, see:

 

Today's UITextView is here to focus on some issues and user-defined issues encountered during UITextView initialization and development. Thank you for your support.

 


From Andy --- breeze

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.