SimpleTextInput sample code - caretRect not calculated correctly at times
SimpleTextInput sample code - caretRect not calculated correctly at times
- Subject: SimpleTextInput sample code - caretRect not calculated correctly at times
- From: Malayil George <email@hidden>
- Date: Mon, 11 Apr 2011 23:22:56 -0400
Hi,
I'm trying to follow the SimpleTextInput example project at
http://developer.apple.com/library/ios/#samplecode/SimpleTextInput/Introduction/Intro.html
For the most part, it works fine, but, for some reason the following
results in a wierd result drawing the caretRect.
1. Type in a few lines (about 7-8 lines in the Simulator (about 10
characters each line followed by a newline)
2. Click on some random point in the text in the last couple of
lines. The caretRect is updated properly to the new location.
3. Click on some random point in the first or second line. The
caret rect isn't drawn correctly and is way larger than usual - If there is
a place I can post a screen shot that is acceptable to the thread, please
let me know and I'd be happy to paste it :-)
In an effort at debugging this, it looks like the ascent and descent
values in caretRectForIndex function changes mid-call. I added a couple of
NSLog statements to the relevant section ( I apologize for the lengthy code
post here). The function is identical to Apple's except for the added NSLog
statements.
- (CGRect)caretRectForIndex:(int)index
{
NSArray *lines = (NSArray *) CTFrameGetLines(_frame);
// Special case, no text
if (_text.length == 0) {
CGPoint origin = CGPointMake(CGRectGetMinX(self.bounds),
CGRectGetMaxY(self.bounds) - self.font.leading);
// Note: using fabs() for typically negative descender from fonts
return CGRectMake(origin.x, origin.y - fabs(self.font.descender), 3,
self.font.ascender + fabs(self.font.descender));
}
// Special case, insertion point at final position in text after newline
if (index == _text.length && [_text characterAtIndex:(index - 1)] ==
'\n') {
CTLineRef line = (CTLineRef) [lines lastObject];
CFRange range = CTLineGetStringRange(line);
CGFloat xPos = CTLineGetOffsetForStringIndex(line, range.location,
NULL);
CGPoint origin;
CGFloat ascent, descent;
CTLineGetTypographicBounds(line, &ascent, &descent, NULL);
NSLog(@"Ascent, Descent: %f, %f", ascent, descent);
CTFrameGetLineOrigins(_frame, CFRangeMake(lines.count - 1, 0),
&origin);
// Place point after last line, including any font leading spacing
if applicable
origin.y -= self.font.leading;
NSLog(@"Ascent, Descent: %f, %f", ascent, descent);
return CGRectMake(xPos, origin.y - descent, 3, ascent +
descent);
}
// Regular case, caret somewhere within our text content range
for (int i = 0; i < [lines count]; i++) {
CTLineRef line = (CTLineRef) [lines objectAtIndex:i];
CFRange range = CTLineGetStringRange(line);
NSInteger localIndex = index - range.location;
if (localIndex >= 0 && localIndex <= range.length) {
// index is in the range for this line
CGFloat xPos = CTLineGetOffsetForStringIndex(line, index, NULL);
CGPoint origin;
CGFloat ascent, descent;
CTLineGetTypographicBounds(line, &ascent, &descent, NULL);
NSLog(@"Ascent, Descent: %f, %f", ascent, descent);
CTFrameGetLineOrigins(_frame, CFRangeMake(i, 0), &origin);
NSLog(@"Ascent, Descent: %f, %f", ascent, descent);
// Make a small "caret" rect at the index position
return CGRectMake(xPos, origin.y - descent, 3, ascent +
descent);
}
}
return CGRectNull;
}
The output from the above method when I click on the first couple of lines
is something like
2011-04-11 23:09:02.670 SimpleTextInput[33460:207] Ascent, Descent:
13.860352, 4.139648
2011-04-11 23:09:02.670 SimpleTextInput[33460:207] Ascent, Descent:
237.339645, 0.000000
The call to CTLineGetTypographicBounds seems to be working fine as
evidenced by the low expected values of ascent and descent. But, for some
reason, after CTFrameGetLineOrigins it is changing. I'm at a loss as to why
these values are changing as there doesn't seem to be anything in the code
modifying ascent and descent between the two NSLog statements.
I've tried synchronizing the caretRect method body on self (but, it
doesn't look like there are other threads that are modifying these values
either).
Any pointers on why ascent and descent is changing would be much
appreciated :)
Thanks
George
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden