UITextView for basic ios controls
The multi-line text control UITextView inherits the UIScrollView: UIView control, which has a scroll bar by default. Both UITextView and UITextField are text editing and display controls. Their functions and behaviors are similar in many aspects. The differences between UITextView and UITextField are as follows: UITextView is a multi-line text box, while UITextField is only a single-line text box. UITextView does not inherit the UIControl Control. Therefore, you cannot bind an IBAction event processing method or call the addTarget: action: forControlEvents: method provided by UIControl to bind an event processing method. UITextView inherits UIScrollView, so it has UIScrollView functions and behaviors.
TextView = [[UITextView alloc] initWithFrame: CGRectMake (100,100,300,300)]; // initialize the size
Self. textView. textColor = [UIColor blackColor]; // font color
Self. textView. font = [UIFont fontWithName: @ "Arial" size: 18.0]; // font type and size
[TextView. layer setCornerRadius: 50]; // sets the rounded corner.
Self. textView. delegate = self; // sets its delegate method.
TextView. layer. borderWidth = 5; // you can specify a border.
Self. textView. backgroundColor = [UIColor redColor]; // background color
Self. textView. text = @ ""; // display content
Self. textView. returnKeyType = UIReturnKeyDefault; // type of the Return key
Self. textView. keyboardType = UIKeyboardTypeDefault; // keyboard type
Self. textView. scrollEnabled = YES; // whether to drag
Self. textView. autocapitalizationType = UIViewAutoresizingFlexibleHeight; // adaptive height
[Self. view addSubview: self. textView];
To disable a keyboard, you can use the following methods:
If there is a navigation bar, add the exit button on the navigation bar:
-(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];
}
Add an exit button on the keyboard:
UIToolbar * topView = [[UIToolbar alloc] initWithFrame: CGRectMake (0, 0,320, 30)];
[TopView setBarStyle: UIBarStyleBlack];
UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithTitle: @ "Done" style: UIBarButtonItemStyleDone target: self action: @ selector (dismissKeyBoard)];
-(IBAction) dismissKeyBoard
{
[TextView resignFirstResponder];
}
You can also modify the ruturn Button Function on the keyboard:
-(BOOL) textView :( UITextView) TextView shouldChangeTextInRange :( nsange) range replacementText :( NSString) Text
{
If ([text isEqualToString: @ "\ n"]) {
[TextView resignFirstResponder];
Return NO;
}
Return YES;
}
Sometimes we select a part of text on UITextView to forward, copy or copy the text. You can add a button in the selection bar:
UIMenuItem * menuItem = [[UIMenuItem alloc] initWithTitle: @ "forward" action: @ selector (changeColor)];
UIMenuController * menu = [UIMenuController sharedMenuController];
[Menu setMenuItems: [NSArray arrayWithObject: menuItem];
-(Void) changeColor
{
// Code for forwarding, copying, or copying specific functions
}
In daily software, we need to extend the length of UITextView as needed in many places.
There are three implementation methods:
-(Void) textViewDidChange :( UITextView *) textView
{
If (textView. text. length> 20) // maximum number of bytes in a row
{
I,
// UIFont * font = [UIFont systemFontOfSize: 15];
// CGSize size = [textView. text sizeWithFont: font constrainedToSize: CGSizeMake (300,100 0) lineBreakMode: UILineBreakModeCharacterWrap];
/// BgImage. frame = CGRectMake (0, 202-size.height + 15,320, size. height + 28 );
// [TextView setFrame: CGRectMake (0, 1,320, size. height + 10)];
** 2 ,**
// CGRect frame = textView. frame;
// CGSize size = [textView. text sizeWithFont: textView. font
// ConstrainedToSize: CGSizeMake (280,100 0)
// LineBreakMode: UILineBreakModeTailTruncation];
// Frame. size. height = size. height> 1? Size. height + 20: 64;
// TextView. frame = frame;
III,
CGRect frame = textView. frame;
Frame. size. height = textView. contentSize. height;
TextView. frame = frame;
}