A summary of Uitextview text input cursor usage techniques in IOS _ios

Source: Internet
Author: User

1. Create and initialize

 @property (nonatomic, strong) Uitextview *textview; 

Create
Self.textview = [[Uitextview alloc] initWithFrame:self.view.frame]; 

Set TextView inside the font color 
 self.textView.textColor = [Uicolor blackcolor]; 
Set font name and font size 
 Self.textView.font = [Uifont fontwithname:@ ' Arial ' size:18.0]; 
Set Agent
 self.textView.delegate = self;  
Set its background color
 self.textView.backgroundColor = [Uicolor whitecolor]; 
 Self.textView.text = @ "hehe"; 
Returns the type of the key 
 self.textView.returnKeyType = Uireturnkeydefault; 
Keyboard type 
 self.textView.keyboardType = Uikeyboardtypedefault; 

Whether you can drag  
self.textView.scrollEnabled = YES;


2. Uitextview exit the keyboard in several ways
(1) If your program has a navigation bar, you can add more than one done button on the navigation bar, used to exit the keyboard, of course, first realize uitextviewdelegate.

-(void) textviewdidbeginediting: (Uitextview *) TextView {  

Self.navigationItem.rightBarButtonItem = [ Uibarbuttonitem Alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemdone target:self action: @selector ( getoverediting)];  

}  
-(void) textviewdidendediting: (Uitextview *) TextView {  
  Self.navigationItem.rightBarButtonItem = nil; 
} 
-(void) getoverediting{
 [Self.textview resignfirstresponder];  
}

(2) If you do not use the ENTER key in the TextView, you can use the Enter to exit the keyboard response keys.

#pragma mark-uitextview Delegate Methods   
-(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: ( Nsrange range Replacementtext: (NSString *) text
{  
 if ([text isequaltostring:@ "\ n"]) {  
   [TextView Resignfirstresponder];  
   return NO;  
  }
 Return YES  
}

(3) You can also customize other view controls to be loaded onto your keyboard to exit, such as adding a view to the pop-up keyboard to place the Done button that exits the keyboard.

   Uitoolbar * TopView = [[Uitoolbar alloc]initwithframe:cgrectmake (0, 0,)]; 
   Uibarbuttonitem * cancelbutton= [[Uibarbuttonitem alloc]initwithtitle:@ "Done" Style:uibarbuttonitemstyledone Target: Self action: @selector (Dismisskeyboard)]; 
   Nsarray * Buttonsarray = @[cancelbutton]; 
   [TopView Setitems:buttonsarray]; 
   [Self.textview Setinputaccessoryview:topview]; 
 -(void) Dismisskeyboard 
 { 
   [Tvtextview resignfirstresponder]; 
 }

3.UITextView Customize the menu after selecting text

Add in Viewdidload:

-(void) viewdidload
{
  [super viewdidload];
  Self._textview = [[Uitextview alloc] Initwithframe:cgrectmake (n, M, MB)];
  [Self.view Addsubview:_textview];  
  UIMenuItem *menuitem = [[UIMenuItem alloc]initwithtitle:@ "I am a custom menu" action: @selector (Didclickcustommenuaction)]; 
  Uimenucontroller *menu = [Uimenucontroller Sharedmenucontroller]; 
  [Menu Setmenuitems:[nsarray Arraywithobject:menuitem]]; 
  [MenuItem release]; 
}

Of course, the above @selector inside the ChangeColor method or write it yourself, that is, click on our custom menu items will trigger the method.
Then you have to add a method to the code:

-(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender 
{ 
  if (action = = @selector (changecolor) | | action = = @selector (copy:) 
  { 
    if (_textview.selectedrange.length>0) return 
      YES; 
  } 
  return NO; 
} 
-(void) didclickcustommenuaction
{
  NSLog (@ "%@", __function__);
}

4. Set Uitextview inner margin
when we use Uitextview as a uilabel for some requirements (in order to use Uitextview's own copy, paste, and select function), we just need to disable several Uitextview attributes.

Textview.editable = no;//not editable 
textview.scrollenabled = no;//not scrollable 
textview.editable = no;//not editable 
textview.scrollenabled = no;//Not scrollable 

This is OK;
But when we actually use it, we want to calculate the size of the text and set the display size of the Uitextview.

Uifont *font = [Uifont systemfontofsize:14.0f]; Specifies the size of the string 
 
[TextView settext:content]; 
 
Cgsize textsize = [content Sizewithfont:font constrainedtosize:cgsizemake (MB) Linebreakmode: Uilinebreakmodecharacterwrap]; 
 
CGRect articleframe = [Articlelabel frame]; 
TextView.size.height = textsize.height; 
 TextView.size.width = Textsize.width; 
[TextView Setframe:articleframe]; 
Uifont *font = [Uifont systemfontofsize:14.0f]; Specifies the size of the string 
 
[TextView settext:content]; 
 
Cgsize textsize = [content Sizewithfont:font constrainedtosize:cgsizemake (MB) Linebreakmode: Uilinebreakmodecharacterwrap]; 
 
CGRect articleframe = [Articlelabel frame]; 
TextView.size.height = textsize.height; 
 TextView.size.width = Textsize.width; 
[TextView Setframe:articleframe]; 

But the use of this method on the Uilabel without any problems, but in the Uitextview is not, the text is always showing incomplete, no matter you actively write more height to it, when the text is not the same double will display incomplete or display height too much;
You can try it in the following way

[Self.articlelabel Setcontentinset:uiedgeinsetsmake (-10,-5,-15,-5)];//set Uitextview's inner margin 
[Self.articlelabel settextalignment:nstextalignmentleft];//and set the left alignment 
[Self.articlelabel setcontentinset:uiedgeinsetsmake (-10,-5,- 15,-5)];//set Uitextview inner margin 
[Self.articlelabel settextalignment:nstextalignmentleft];//and set left-aligned 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.