简体   繁体   中英

Assign Some Text to particular line in UITextView Programmatically

I create an instance of UITextView programmatically. I want to assign some text to particular line in UITextView programmatically. Here is my code to create UITextView.

UITextView *textView =[[UITextView alloc]init];
textView.frame=CGRectMake(0,0,282,210);
[textView setReturnKeyType:UIReturnKeyDone];
[self.view addSubview:textView];

For example I want to add some text to particular line (at the end of line 5).

Programmatically, how is it possible to do this?

UITextView *textView =[[UITextView alloc]init];
textView.frame=CGRectMake(0,0,282,210);  
textView.text = @"xxxxxx";

...

You can't edit lines directly in the text view because the number of lines depends on the content size and the size of the font.

Check this question as a reference: How to Read Number of lines in UITextView

You could do some calculation to determine where to insert the text, but I'm nor sure if that's the best way to go around. There is probably a better way to accomplish what you are trying to do.

you can use the following:

NSString *str=yourTextview.text;
[str stringByAppendingString:yourNewString];

Then add it to textview

yourTextView.text=str;

check it and let me know if any clarification you need

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM