0

Ultimately, I would like to know the position of the last character in a UITextView. I was able to find out the y position by using the following:

CGSize size = [textView.text sizeWithFont:textView.font constrainedToSize:CGSizeMake(textView.frame.size.width-16, 10000) lineBreakMode:UILineBreakModeClip];

However in order to get the x position I need to get the text from the last line in the textview. I tried removing a character at a time from textView.text until size.height changes to achieve that effect, but I realize that does not always equal to what I want when linebreak is taken into consideration.

Any suggestions anyone? I've been spending a whole day trying to figure this one out...

2 Answers2

2

I think I found a solution by using caretRectForPosition to get the pixel position of the cursor in the textview.

// move cursor to the end of the text
textView.selectedRange = NSMakeRange([textView.text length], 0);
CGPoint cursorPosition = [textView caretRectForPosition:textView.selectedTextRange.start].origin;
0

you can evaluate the text size in a uitextview or uitextfield like this

    UIFont * myFont = [UIFont fontWithName:@"Gill Sans" size:19];
    CGSize textSize = [@"your text" sizeWithFont: myFont constrainedToSize:CGSizeMake(textFiled.frame.size.width, textFiled.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
    NSlog(@"this is your text size W=%f H=%f",textSize.width, textSize.height);

touti
  • 1,164
  • 6
  • 18