当前位置:网站首页>Some tips for using uitextview

Some tips for using uitextview

2022-06-24 02:25:00 Juming 12255

1. Insert a string at the specified position :

NSMutableString *TextViewStr=[[NSMutableString alloc] initWithString:TextView.text];
        [TextViewStr insertString:@"your strings" atIndex:TextView.selectedRange.location];
        TextView.scrollEnabled=NO;
        TextView.text=theTvStr;
         theTV.scrollEnabled=YES;

2. Get rows ( Including line breaks ):

CGSize size = [[self.TextView text] sizeWithFont:[self.TextView font]];

   // 2.  Height of extracted text 
    int length = size.height;

    //3.  Count lines 
    int colomNumber = TextView.contentSize.height/length;

3. Detect line breaks :

- (BOOL)textView: (UITextView *)textview shouldChangeTextInRange: (NSRange)range replacementText: (NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        NSLog(@"it is a row !!");
        //...
    }
    return YES;
}<pre name="code" class="objc"> self.textView = [[[UITextView  alloc] initWithFrame:self.view.frame] autorelease]; // Initialize size and automatically release 

 self.textView.textColor = [UIColor blackColor];// Set up textview The font color inside  

 self.textView.font = [UIFont fontWithName:@"Arial" size:18.0];// Set font name and font size  

 self.textView.delegate = self;// Set its delegate method  

 self.textView.backgroundColor = [UIColor whiteColor];// Set its background color 

 self.textView.text = @"Now is the time for all good developers to come to serve their country.\n\nNow is the time for all good developers to come to serve their country.";// Set what it shows  

 self.textView.returnKeyType = UIReturnKeyDefault;// Return the type of key  

 self.textView.keyboardType = UIKeyboardTypeDefault;// Keyboard type  

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

 self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;// Adaptive height 

 [self.view addSubview: self.textView];// Add to the whole page 

notes :

The text field implements UITextInputTrait agreement , It provides 7 Attributes to define how the field handles text input :autocapitalizationType、 autocorrectionType、enablesReturnKeyAutomatically、keyboardAppearance、 keyboardType、returnKeyType、secureTextEntry. Other , When the text field is empty ,placeholder The text is grayed out , Provide a user prompt . By setting clearButtonMode You can specify whether and when to display the clear button .

If your textview You don't need to press enter in the , You can use the Enter key as a response key to exit the keyboard .

#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; 
}
原网站

版权声明
本文为[Juming 12255]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/11/20211101101003965h.html