UITextView insert text in the textview text

后端 未结 6 1008
谎友^
谎友^ 2020-12-10 03:16

I want to have to occasionally insert text into the UITextView text object. For example, if the user presses the \"New Paragraph\" button I would like to insert a double ne

6条回答
  •  -上瘾入骨i
    2020-12-10 03:38

    Here's how I implemented it and it seems to work nicely.

    - (void) insertString: (NSString *) insertingString intoTextView: (UITextView *) textView  
    {  
        NSRange range = textView.selectedRange;  
        NSString * firstHalfString = [textView.text substringToIndex:range.location];  
        NSString * secondHalfString = [textView.text substringFromIndex: range.location];  
        textView.scrollEnabled = NO;  // turn off scrolling or you'll get dizzy ... I promise  
    
        textView.text = [NSString stringWithFormat: @"%@%@%@",  
          firstHalfString,  
          insertingString,  
          secondHalfString];  
        range.location += [insertingString length];  
        textView.selectedRange = range;  
        textView.scrollEnabled = YES;  // turn scrolling back on.  
    
    }
    

提交回复
热议问题