多行文本控件UITextView继承UIScrollView:UIView控件,默认是带有滚动条的。UITextView和UITextField都是文本编辑、显示控件,它们功能和行为在很多方面都是相似的。UITextView与UITextField的区别主要在以下三点: UITextView是一个多行文本框,而UITextField只是单行文本框。 UITextView没有继承UIControl控件,因此不能绑定IBAction事件处理 方法;也不能调用UIControl提供的addTarget:action:forControlEvents:方法来绑定事件处理的方法。 UITextView继承了UIScrollView,因此具有UIScrollView的功能和行为。
Textview=[[uitextview alloc]initwithframe:cgrectmake (100, 100, 300, 300)];//Initialize dimensions
Self.textview.textcolor=[uicolor blackcolor];//Font Color
Self.textview.font=[uifont fontwithname:@ "Arial" size:18.0];//font type and size
[Textview.layer setcornerradius:50];//setting rounded corners
self.textview.delegate=self;//to set its delegate method
textview.layer.borderwidth=5;//setting borders
Self.textview.backgroundcolor=[uicolor redcolor];//Background color
[Email protected] "";//display content
self.textview.returnkeytype=uireturnkeydefault;//type of Return key
self.textview.keyboardtype=uikeyboardtypedefault;//keyboard Type
self.textview.scrollenabled=yes;//Whether you can drag
self.textview.autocapitalizationtype=uiviewautoresizingflexibleheight;//Self-adapting height
[Self.view AddSubview:self.textView];
There are several ways to turn off the keyboard:
If you have a navigation bar, add an Exit button to 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 to the top of 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 function of the Ruturn button on the keyboard:
-(BOOL) TextView: (Uitextview ) TextView shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString ) Text
{
if ([text isequaltostring:@ "\ n"]) {
[TextView Resignfirstresponder];
return NO;
}
return YES;
}
Sometimes we choose to forward, copy or copy some of the text on Uitextview. You can add a button to the selection bar:
UIMenuItem *menuitem=[[uimenuitem alloc]initwithtitle:@ "forward" Action: @selector (ChangeColor)];
Uimenucontroller *menu=[uimenucontroller Sharedmenucontroller];
[Menu Setmenuitems:[nsarray Arraywithobject:menuitem];
-(void) ChangeColor
{
Implement code that forwards, copies, or copies specific functions
}
In everyday software we need to extend the length of the Uitextview as needed to meet the needs.
Here are three ways to implement this:
-(void) Textviewdidchange: (Uitextview *) TextView
{
if (TextView.text.length > 20)//row up to how many bytes
{
First,
Uifont *font = [Uifont systemfontofsize:15];
Cgsize size = [Textview.text sizewithfont:font constrainedtosize:cgsizemake (+) Linebreakmode: Uilinebreakmodecharacterwrap];
Bgimage.frame = CGRectMake (0, 202-size.height+15, size.height+28);
[TextView setframe:cgrectmake (0, 1, size.height+10)];
**二、**
CGRect frame = textview.frame;
Cgsize size = [Textview.text sizeWithFont:textView.font
Constrainedtosize:cgsizemake (280, 1000)
Linebreakmode:uilinebreakmodetailtruncation];
Frame.size.height = size.height > 1? Size.Height + 20:64;
Textview.frame = frame;
Third,
CGRect frame = textview.frame;
Frame.size.height = TextView.contentSize.height;
Textview.frame = frame;
}
iOS base Control Uitextview