#import"TextViewController.h"@interface Textviewcontroller ()<UITextViewDelegate>@end @implementation Textviewcontroller/*uitextview text View compared with Uitextfield visual difference is that Uitextview can enter multiple lines of text and can scroll to view the full text. Common Uitextview are used in the app's software introduction, content details display, novel reading display, publishing space content input, speaking text box, comment text box and so on. The use of Uitextview has its own proxy method, and there are methods that inherit from the parent class. The method itself has the whole process of listening from the beginning of editing to the end of the edit, the method of inheritance is mainly inherited from the Uiscrollview method, because the control of the scroll belongs to Uiscrollview. */- (void) viewdidload {[Super viewdidload]; //InitializeUitextview *textview = [[Uitextview alloc] Initwithframe:cgrectmake ( -, -, -, -)]; Textview.tag= -; //Background ColorTextview.backgroundcolor=[Uicolor Yellowcolor]; //Configure text Properties//1. Set the text of the TextViewTextview.text =@"Where There is a would, there is a way!"; //in fact, Uitextview text by default is the home display, the above situation is many because the use of Navigationcontroller let ScrollView automatically adapt to the screen caused. (Uitextview inherits from Uiscrollview). if([Self respondstoselector: @selector (setautomaticallyadjustsscrollviewinsets:)]) {Self.automaticallyad Justsscrollviewinsets=NO; } //2. Set the text alignment (default is Nstextalignmentleft)Textview.textalignment =Nstextalignmentleft; //3. Set the font and size of the text (the default font is 17 dots Helvetica normal font)Textview.font = [Uifont fontwithname:@"Marker Felt"Size -]; //4. Set the color of the fontTextview.textcolor =[Uicolor Magentacolor]; //5. Whether edits are allowed (default = yes)Textview.editable =YES; //6. Set text can be selectedTextview.selectable =YES; //7. Return the type of the key (default is Uikeyuireturnkeydefault)Textview.returnkeytype =Uireturnkeydefault; //8. Keyboard typeTextview.keyboardtype =Uikeyboardtypedefault; //9. Whether you can dragtextview.scrollenabled =YES; //10. Set to Border angle RadianTextView.layer.cornerRadius =5; //11. Set the border widthTextView.layer.borderWidth =1.0; //12.UITextView Customizing the menu after selecting textUIMenuItem *menuitem = [[UIMenuItem alloc]initwithtitle:@"Share to Space"Action: @selector (ChangeColor:)]; Uimenucontroller*menu =[Uimenucontroller Sharedmenucontroller]; [Menu Setmenuitems:[nsarray Arraywithobject:menuitem]; [MenuItem release]; //Set up proxyTextView.Delegate=Self ; [Self.view Addsubview:textview]; [TextView release]; }#pragmaMark---wuyiyi-(void) ChangeColor: (UIMenuItem *) Item {}#pragmaMark---uitextviewdelegate//triggered when editing is about to begin-(BOOL) textviewshouldbeginediting: (Uitextview *) TextView {NSLog (@"%s,%d", __function__, __line__); returnYES;}//triggered when editing is about to end-(BOOL) textviewshouldendediting: (Uitextview *) TextView {NSLog (@"%s,%d", __function__, __line__); returnYES;}//triggered when editing has started- (void) Textviewdidbeginediting: (Uitextview *) TextView {NSLog (@"%s,%d", __function__, __line__); //Recycle keyboard//(1) If the program has a navigation bar, you can add a done button on the navigation bar to exit the keyboardUibarbuttonitem *done =[[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemdone target:self action: @selector ( Leveleditmode:)]; Self.navigationItem.rightBarButtonItem=Done ; [Done release];}//triggered when an edit has ended- (void) Textviewdidendediting: (Uitextview *) TextView {NSLog (@"%s,%d", __function__, __line__); //end Edit, remove done buttonSelf.navigationItem.rightBarButtonItem =Nil;}//trigger when changing text-(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) Text {NSLog (@"%s,%d", __function__, __line__); //(2) Click Return to recycle the keyboard if([Text isequaltostring:@"\ n"]) {[TextView resignfirstresponder]; } returnYES;//return NO;}//change the position of the Uitextview cursor:- (void) Textviewdidchangeselection: (Uitextview *) TextView {Nsrange range; Range.location=0; Range.length=0; Textview.selectedrange=range;}#pragmaMark----Done Action//Click the button to exit the keyboard method- (void) Leveleditmode: (Uibarbuttonitem *) Sender {Uitextview*TEXTV = (Uitextview *) [Self.view Viewwithtag: -]; [Textv Resignfirstresponder];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.} @end
Uitextview in iOS