-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
485 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// DTCoreTextLayoutFrame+Cursor.h | ||
// DTCoreText | ||
// | ||
// Created by Oliver Drobnik on 10.07.13. | ||
// Copyright (c) 2013 Drobnik.com. All rights reserved. | ||
// | ||
|
||
#import "DTCoreTextLayoutFrame.h" | ||
|
||
/** | ||
The **Cursor** category extends DTCoreTextLayoutFrame for working with a caret and determine the string index of touch coordinates. | ||
*/ | ||
|
||
@interface DTCoreTextLayoutFrame (Cursor) | ||
|
||
/** | ||
Determines the closest string index to a point in the receiver's frame. | ||
This can be used to find the cursor position to position an input caret at. | ||
@param point The point | ||
@returns The resulting string index | ||
*/ | ||
- (NSInteger)closestCursorIndexToPoint:(CGPoint)point; | ||
|
||
/** | ||
The rectangle to draw a caret for a given index | ||
@param index The string index for which to determine a cursor frame | ||
@returns The cursor rectangle | ||
*/ | ||
- (CGRect)cursorRectAtIndex:(NSInteger)index; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// | ||
// DTCoreTextLayoutFrame+Cursor.m | ||
// DTCoreText | ||
// | ||
// Created by Oliver Drobnik on 10.07.13. | ||
// Copyright (c) 2013 Drobnik.com. All rights reserved. | ||
// | ||
|
||
#import "DTCoreTextLayoutFrame+Cursor.h" | ||
#import "DTCoreTextLayoutLine.h" | ||
|
||
@implementation DTCoreTextLayoutFrame (Cursor) | ||
|
||
- (NSInteger)closestCursorIndexToPoint:(CGPoint)point | ||
{ | ||
NSArray *lines = self.lines; | ||
|
||
if (![lines count]) | ||
{ | ||
return NSNotFound; | ||
} | ||
|
||
DTCoreTextLayoutLine *firstLine = [lines objectAtIndex:0]; | ||
if (point.y < CGRectGetMinY(firstLine.frame)) | ||
{ | ||
return 0; | ||
} | ||
|
||
DTCoreTextLayoutLine *lastLine = [lines lastObject]; | ||
if (point.y > CGRectGetMaxY(lastLine.frame)) | ||
{ | ||
NSRange stringRange = [self visibleStringRange]; | ||
|
||
if (stringRange.length) | ||
{ | ||
return NSMaxRange([self visibleStringRange])-1; | ||
} | ||
} | ||
|
||
// find closest line | ||
DTCoreTextLayoutLine *closestLine = nil; | ||
CGFloat closestDistance = CGFLOAT_MAX; | ||
|
||
for (DTCoreTextLayoutLine *oneLine in lines) | ||
{ | ||
// line contains point | ||
if (CGRectGetMinY(oneLine.frame) <= point.y && CGRectGetMaxY(oneLine.frame) >= point.y) | ||
{ | ||
closestLine = oneLine; | ||
break; | ||
} | ||
|
||
CGFloat top = CGRectGetMinY(oneLine.frame); | ||
CGFloat bottom = CGRectGetMaxY(oneLine.frame); | ||
|
||
CGFloat distance = CGFLOAT_MAX; | ||
|
||
if (top > point.y) | ||
{ | ||
distance = top - point.y; | ||
} | ||
else if (bottom < point.y) | ||
{ | ||
distance = point.y - bottom; | ||
} | ||
|
||
if (distance < closestDistance) | ||
{ | ||
closestLine = oneLine; | ||
closestDistance = distance; | ||
} | ||
} | ||
|
||
if (!closestLine) | ||
{ | ||
return NSNotFound; | ||
} | ||
|
||
NSInteger closestIndex = [closestLine stringIndexForPosition:point]; | ||
|
||
NSInteger maxIndex = NSMaxRange([closestLine stringRange])-1; | ||
|
||
if (closestIndex > maxIndex) | ||
{ | ||
closestIndex = maxIndex; | ||
} | ||
|
||
if (closestIndex>=0) | ||
{ | ||
return closestIndex; | ||
} | ||
|
||
return NSNotFound; | ||
} | ||
|
||
- (CGRect)cursorRectAtIndex:(NSInteger)index | ||
{ | ||
DTCoreTextLayoutLine *line = [self lineContainingIndex:index]; | ||
|
||
if (!line) | ||
{ | ||
return CGRectZero; | ||
} | ||
|
||
CGFloat offset = [line offsetForStringIndex:index]; | ||
|
||
CGRect rect = line.frame; | ||
rect.size.width = 3.0; | ||
rect.origin.x += offset; | ||
|
||
return rect; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.