On May 22, 2013, at 8:05 AM, Koen van der Drift <koenvanderdr...@gmail.com> wrote:
> I'm looking to add some highlighting to my Core Text View, and implemented > the following which comes straight from Apple's SimpleTextInput code example > (https://developer.apple.com/library/iOs/#samplecode/SimpleTextInput/Introduction/Intro.html): > > // Helper method for drawing the current selection range (as a simple filled > rect) > - (void)drawRangeAsSelection:(NSRange)selectionRange > { > // If not in editing mode, we do not draw selection rects > if (!self.editing) > return; > > // If selection range empty, do not draw > if (selectionRange.length == 0 || selectionRange.location == NSNotFound) > return; > > // set the fill color to the selection color > [[SimpleCoreTextView selectionColor] setFill]; > > // Iterate over the lines in our CTFrame, looking for lines that > intersect > // with the given selection range, and draw a selection rect for each > intersection > NSArray *lines = (NSArray *) CTFrameGetLines(_frame); > for (int i = 0; i < [lines count]; i++) { > CTLineRef line = (CTLineRef) [lines objectAtIndex:i]; > CFRange lineRange = CTLineGetStringRange(line); > NSRange range = NSMakeRange(lineRange.location, lineRange.length); > NSRange intersection = [self RangeIntersection:range > withSecond:selectionRange]; > if (intersection.location != NSNotFound && intersection.length > 0) { > // The text range for this line intersects our > selection range > CGFloat xStart = CTLineGetOffsetForStringIndex(line, > intersection.location, NULL); > CGFloat xEnd = CTLineGetOffsetForStringIndex(line, > intersection.location + intersection.length, NULL); > CGPoint origin; > // Get coordinate and bounds information for the > intersection text range > CTFrameGetLineOrigins(_frame, CFRangeMake(i, 0), &origin); > CGFloat ascent, descent; > CTLineGetTypographicBounds(line, &ascent, &descent, NULL); > <<<<<<<<<=========================== BOOM! > // Create a rect for the intersection and draw it with > selection color > CGRect selectionRect = CGRectMake(xStart, origin.y - descent, xEnd > - xStart, ascent + descent); > UIRectFill(selectionRect); > } > } > } > > > > But every time, I get an EXC_BAD_ACCESS error for the > CTLineGetTypographicBounds() call. I triple checked that _frame and line are > valid at this point, and the debugger output shows what it is supposed to be > (correct string, attributes, etc). > > Here's the output from bt: > > (lldb) bt > * thread #1: tid = 0x1c03, 0x0027bbd0 CoreText`CTLineGetTypographicBounds + > 29, stop reason = EXC_BAD_ACCESS (code=2, address=0x60) > frame #0: 0x0027bbd0 CoreText`CTLineGetTypographicBounds + 29 > frame #1: 0x000469a4 MyApp`-[MyView > drawRangeAsSelection:](self=0x4576f000, _cmd=0x00000000, > selectionRange=(null)) + 628 at MyView.m:461 > > > Any suggestions why this could be happening? The preceding call looks like it could be problematic: > CTFrameGetLineOrigins(_frame, CFRangeMake(i, 0), &origin); If you want a single line origin (CGPoint), looks like the length of the range passed in should be 1, not 0. >From >https://developer.apple.com/library/mac/#documentation/Carbon/reference/CTFrameRef/Reference/reference.html > range > The range of line origins you wish to copy. If the length of the range is 0, > then the copy operation continues from the start index of the range to the > last line origin. _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com