Skip to content

Instantly share code, notes, and snippets.

@pita5
Created December 19, 2011 13:29
Show Gist options
  • Save pita5/1497238 to your computer and use it in GitHub Desktop.
Save pita5/1497238 to your computer and use it in GitHub Desktop.
// Public method to determine the CGRect for the insertion point or selection, used
// when creating or updating our SimpleCaretView instance
- (CGRect)caretRectForIndex:(int)index
{
if (_editingFrame)
{
CTFrameRef _frame = (CTFrameRef)[_editingFrame objectForKey:@"frame"];
NSArray *lines = (NSArray *) CTFrameGetLines(_frame);
NSString *text = [_editingFrame objectForKey:@"text"];
// Special case, no text
if (text.length == 0)
{
CGPoint origin = CGPointMake(CGRectGetMinX(self.bounds), CGRectGetMaxY(self.bounds) - 10);
// Note: using fabs() for typically negative descender from fonts
return CGRectMake(origin.x, origin.y, 3, 10);
}
// 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);
CTFrameGetLineOrigins(_frame, CFRangeMake(lines.count - 1, 0), &origin);
// Place point after last line, including any font leading spacing if applicable
origin.y -= 10;
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);
CTFrameGetLineOrigins(_frame, CFRangeMake(i, 0), &origin);
// Make a small "caret" rect at the index position
NSLog(@"returning");
NSLog(@"%@", NSStringFromCGRect(CGRectMake(xPos, origin.y - descent, 3, ascent + descent)));
return CGRectMake(xPos, origin.y - descent, 3, ascent + descent);
}
}
}
return CGRectNull;
}
GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Mon Aug 15 16:03:10 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 25246.
2011-12-19 13:26:41.435 CTRenderer[25246:207] returning
2011-12-19 13:26:41.436 CTRenderer[25246:207] {{0, 436.74}, {3, 23.6}}
warning: check_safe_call: could not restore current frame
warning: Unable to restore previously selected frame.
warning: Unable to restore previously selected frame.
Cannot access memory at address 0x42f80000
warning: Unable to restore previously selected frame.
warning: Unable to restore previously selected frame.
warning: Unable to restore previously selected frame.
warning: check_safe_call: could not restore current frame
warning: Unable to restore previously selected frame.
warning: Unable to restore previously selected frame.
warning: Unable to restore previously selected frame.
(gdb) bt
#0 0x42f80000 in ?? ()
// Helper method to update caretView when insertion point/selection changes
- (void)selectionChanged
{
// If not in editing mode, we don't show the caret
if (!self.editing)
{
[_caretView removeFromSuperview];
return;
}
// If there is no selection range (always true for this sample), find the
// insert point rect and create a caretView to draw the caret at this position
if (self.__selectedTextRange.length == 0)
{
CGRect f = [self caretRectForIndex:self.__selectedTextRange.location];
NSLog(@"%@", NSStringFromCGRect(f));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment