The UITextView text input cursor in iOS USES a tip summary

  • 2020-06-12 10:42:21
  • OfStack

1. Create and initialize


 @property (nonatomic, strong) UITextView *textView; 

//  create 
self.textView = [[UITextView alloc] initWithFrame:self.view.frame]; 

//  Set up the textview The font color inside  
 self.textView.textColor = [UIColor blackColor]; 
//  Sets the font name and font size  
 self.textView.font = [UIFont fontWithName:@"Arial" size:18.0]; 
//  Set the 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; 
//  The keyboard type  
 self.textView.keyboardType = UIKeyboardTypeDefault; 

//  Can I drag   
self.textView.scrollEnabled = YES;


There are several ways to exit the UITextView keyboard
(1) If your program has a navigation bar, you can add an Done button on the navigation bar to exit the keyboard. Of course, you should implement UITextViewDelegate first.


- (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 don't use the enter key in textview, use it as an exit response.


#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 attached to the keyboard for exit, such as adding an view button to the pop-up keyboard to hold the Done button for exit.


   UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)]; 
   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]; 
 }

UITextView customizes the menu after the text

Add to ViewDidLoad:


- (void)viewDidLoad
{
  [super viewDidLoad];
  self._textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 100, 300, 200)];
  [self.view addSubview:_textView];  
  UIMenuItem *menuItem = [[UIMenuItem alloc]initWithTitle:@ "I'm a custom menu " action:@selector(didClickCustomMenuAction)]; 
  UIMenuController *menu = [UIMenuController sharedMenuController]; 
  [menu setMenuItems:[NSArray arrayWithObject:menuItem]]; 
  [menuItem release]; 
}

Of course, the changeColor method in @selector above should be written by ourselves, which means that the method will be triggered when we click on our custom menu item.
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 the UITextView padding
When we use UITextView as UILabel for 1 requirement (in order to use UITextView's built-in copy, paste, select), we only need to disable a few attributes of UITextView


textView.editable = NO;// Do not edit  
textView.scrollEnabled = NO;// Do not roll  
textView.editable = NO;// Do not edit  
textView.scrollEnabled = NO;// Do not roll  

And so on.
But in practice, we want to calculate the text size and set the UITextView display size


UIFont *font = [UIFont systemFontOfSize:14.0f]; // Specifies the size of the string  
 
[textView setText:content]; 
 
CGSize textSize = [content sizeWithFont:font constrainedToSize:CGSizeMake(200, 2000) 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(200, 2000) lineBreakMode:UILineBreakModeCharacterWrap]; 
 
CGRect articleframe = [articleLabel frame]; 
textView.size.height = textSize.height ; 
 textView.size.width = textSize.width; 
[textView setFrame:articleframe]; 

However, there is no problem with using this method on UILabel, but it does not work on UITextView. The text is always not fully displayed. No matter how much height you actively write to it, the text will not be fully displayed or display too much height.
You can try this one the following way


[self.articleLabel setContentInset:UIEdgeInsetsMake(-10, -5, -15, -5)];// Set up the UITextView The padding  
[self.articleLabel setTextAlignment:NSTextAlignmentLeft];// And set the left alignment  
[self.articleLabel setContentInset:UIEdgeInsetsMake(-10, -5, -15, -5)];// Set up the UITextView The padding  
[self.articleLabel setTextAlignment:NSTextAlignmentLeft];// And set the left alignment  


Related articles: