IOS control album
UITextView Control
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>
@ Property (nonatomic, weak) UITextView * textView;
@ End
// ================================================ ==========================================================
Initialize the textview in the. m file and write the following code:
// Initialize the size and release it automatically
Self. textView = [[UITextView alloc] initWithFrame: self. view. frame];
// Set the font color in textview
Self. textView. textColor = [UIColor blackColor];
// Set the font name and font size
Self. textView. font = [UIFont fontWithName: @ "Arial" size: 18.0];
// Set its delegate Method
Self. textView. delegate = self;
// Set the background color.
Self. textView. backgroundColor = [UIColor whiteColor];
// Set the display content
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. ";
// Type of the Return key
Self. textView. returnKeyType = UIReturnKeyDefault;
// Keyboard type
Self. textView. keyboardType = UIKeyboardTypeDefault;
// Whether the image can be dragged
Self. textView. scrollEnabled = YES;
// Adaptive height
Self. textView. autoresizingMask = UIViewAutoresizingFlexibleHeight;
// Add to the entire page
[Self. view addSubview: self. textView];
2. Set UITextView rounded corners
Reference # After importing QuartzCore/QuartzCore. h,
Call [textView. layer setCornerRadius: 10]; set the UITextView authorization Angle
3. 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.0f]
ConstrainedToSize: CGSizeMake (FLT_MAX, 21.0)
LineBreakMode: NSLineBreakByWordWrapping];
4. 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)];
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 (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 www.2cto.com
Target: self action: @ selector (dismissKeyBoard)];
NSArray * buttonsArray = @ [helloButton, btnSpace, doneButton];
[TopView setItems: buttonsArray];
[TvTextView setInputAccessoryView: topView];
}
-(Void) dismissKeyBoard
{
[TvTextView resignFirstResponder];
}