Wednesday, June 13, 2012

Method to insert text to a UITextView.


Method to insert text to a UITextView.

- (void)insertTextToTextView:(NSString *)string
{
    if ([self.textView.text length]== 0)// Nothing in the textview
    {
        NSString *textViewString = [[NSString alloc] initWithFormat:@"%@%@",textView.text, string];
        [textView setText:textViewString];
    }
    else
    {
        NSRange range = textView.selectedRange;
        NSString *firstHalfString = [textView.text substringToIndex:range.location];
        NSString *secondHalfString = [textView.text substringFromIndex:range.location];
        textView.text = [NSString stringWithFormat:@"%@%@%@",firstHalfString,string,secondHalfString];
        textView.selectedRange = NSMakeRange(firstHalfString.length+string.length,0);
       
    }
}

No comments:

Post a Comment