(1) The difference between Uitextview and Uitextfield can (simply) be understood as, the former is a region can write a lot of things have a newline scroll bar god horse, the latter only a line of text, similar to the HTML inside the textarea and text.
(2) In this example, we use a uitextfield and a UIButton to continually add the contents of the Uitextfield to the last line of the Uitextview. Here, add an event to the button and add the Uitextfield.text to the back of the Uitextview.text in the event.
(3) In this example, in order to user experience, you can set each increment and decrement the content, the cursor or text box is the last line of reality, where the Scrollrangetovisible method is used, this method needs a range parameter, We can get Uitextview Selectedrange and pass the past as parameters. This range is a struct structure consisting of two parameters of position and length, somewhat similar to the Cgpoint and Cgsize feel.
(4) Also take advantage of the addition of the Uitextviewdelegate protocol in AppDelegate.h to learn about the ways in which there are several options for editing, ending editing, editing and ending edits, and so on.
(5) There are several protocol methods that are important, as they relate to whether the content can be changed, when the content changes, or when the position or length of the seletedrange (i.e., the cursor position changes and additions or deletions) changes. This one...... Uh...... It's very practical. Of course, you can also output the changed content and so on.
#import "ViewController.h" @interface Viewcontroller () @end @implementation viewcontroller{Uitextview *textview1; Uitextfield *textfield1;} -(void) Viewdidload {//Initialize Textview1=[[uitextview Alloc]init]; Set the size position Textview1.frame=cgrectmake (10, 30, 300, 100); Set background color Textview1.backgroundcolor=[uicolor Orangecolor]; Set text content [email protected] "What should I say to U,my lovely World,hello,anybody home. I'm a bad guy. I don't know what to enter! Do you want to continue typing? Do you really? Hello,aggin,here am I.what is you doing,man?where is U going man? "; Set font size, bold and italic, etc. textview1.font=[uifont boldsystemfontofsize:20]; After the above settings, the text over the box, we can scroll up and down to see, and we can continue to enter the addition of delete and so on, you can copy and cut (double-click Text)//can also be line-up input, and so on, which is the difference between TextField//if editable is set to No, You can not add delete and cut, only copy//This time the keyboard also does not come out, can only double-click text Copy textview1.editable=yes; After adding uitextviewdelegate to the appdelegate, textview1.delegate=self; [Self.view Addsubview:textview1]; We add a Uitextfield and UIButton, enter the content in the Uitextfield, and then add the content to the Uitextview by adding the buttonTextfield1=[[uitextfield Alloc]init]; Textfield1.frame=cgrectmake (10, 150, 300, 30); Textfield1.borderstyle=uitextborderstyleroundedrect; [Self.view Addsubview:textfield1]; UIButton *btn1=[uibutton Buttonwithtype:uibuttontyperoundedrect]; Btn1.frame=cgrectmake (10, 200, 300, 30); [Btn1 settitle:@ "Add" forstate:uicontrolstatenormal]; Btn1.backgroundcolor=[uicolor Orangecolor]; [Self.view ADDSUBVIEW:BTN1]; Add an event to BTN1 [btn1 addtarget:self Action: @selector (ADDTEXT1) forcontrolevents:uicontroleventtouchupinside]; [Super Viewdidload]; Do any additional setup after loading the view, typically from a nib.} -(void) addtext1{//Note function is Stringbyappendingformat textview1.text=[textview1.text stringbyappendingformat:@ "\n%@", Textfield1.text]; After adding the cursor positioned at the end of the//slectedrange is a structure that represents the position and length, followed by the Nsmakerange same//nsuinteger representing the unsigned integer, but with the following two lines set invalid, because Selectedrange is an attribute, You can get the value or you can assign a value but not the method can not manipulate the scrolling action//nsuinteger len1=textview1.text.length-1; Textview1.selectedraNge=nsmakerange (len1, 0); Need to use a scrolling operation method, and the Selectedrange property value as a range value to pass in//dissatisfied with each new addition after it all to scroll from top to tail, it feels strange [textView1 scrollrangetovisible: Textview1.selectedrange];} The following four protocols are in the same manner as in the uitextfielddelegate, omitting to say//-(BOOL) textviewshouldbeginediting: (Uitextview *) textview;//-(BOOL) Textviewshouldendediting: (Uitextview *) textview;//-(void) textviewdidbeginediting: (Uitextview *) textView;//-(void ) Textviewdidendediting: (Uitextview *) textview;//a look is BOOL, either Yes or no, is allowed to modify the content-(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) text{return YES; Only triggered when the content changes, and this change is the manual input is valid, in this case the button to increase the content does not trigger this operation-(void) Textviewdidchange: (Uitextview *) textview{NSLog (@ "did Change ");} Almost all actions trigger textviewdidchangeselection, including tapping the text box, adding content to delete content//Can be understood as long as the selectedrange is relevant to the trigger, (position and length)-(void) Textviewdidchangeselection: (Uitextview *) textview{NSLog (@ "did change Selection");} @end
Cut a Picture:
"iOS Dev-17" Uitextview properties, click on new case, and how to change cursor/value in Uitextviewdelegate