, , NSTextView selectionRangeForProposedRange:granularity: . Apple "Cocoa " doc (https://developer.apple.com/library/prerelease/mac/documentation/TextFonts/Conceptual/CocoaTextArchitecture/TextEditing/TextEditing.html - . " NSTextView" ). Apple : " arent (, )." , Apple , , , selectionRangeForProposedRange:granularity: , , , - , ; - , . , , ; . , Apple , . , , "", . ; Apple, .
, Apple doc : " ( ) ". , , : doubleClickAtIndex: NSAttributedString ( NSAttributedStringKitAdditions). ( NSTextStorage NSAttributedString) Cocoa . NSTextStorage , MyTextStorage. NSTextStorage Apple.
MyTextStorage .h:
@interface MyTextStorage : NSTextStorage
- (id)init;
- (id)initWithAttributedString:(NSAttributedString *)attrStr;
@end
MyTextStorage.m:
@interface MyTextStorage ()
{
NSMutableAttributedString *contents;
}
@end
@implementation MyTextStorage
- (id)initWithAttributedString:(NSAttributedString *)attrStr
{
if (self = [super init])
{
contents = attrStr ? [attrStr mutableCopy] : [[NSMutableAttributedString alloc] init];
}
return self;
}
- init
{
return [self initWithAttributedString:nil];
}
- (void)dealloc
{
[contents release];
[super dealloc];
}
- (NSString *)string
{
return [contents string];
}
- (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRange *)range
{
return [contents attributesAtIndex:location effectiveRange:range];
}
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str
{
NSUInteger origLen = [self length];
[contents replaceCharactersInRange:range withString:str];
[self edited:NSTextStorageEditedCharacters range:range changeInLength:[self length] - origLen];
}
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range
{
[contents setAttributes:attrs range:range];
[self edited:NSTextStorageEditedAttributes range:range changeInLength:0];
}
- (NSRange)doubleClickAtIndex:(NSUInteger)location
{
NSRange superRange = [super doubleClickAtIndex:location];
NSString *string = [self string];
if ([string characterAtIndex:location] == '.')
return NSMakeRange(location, 1);
NSRange candidateRangeBeforeLocation = NSMakeRange(superRange.location, location - superRange.location);
NSRange candidateRangeAfterLocation = NSMakeRange(location + 1, NSMaxRange(superRange) - (location + 1));
NSRange periodBeforeRange = [string rangeOfString:@"." options:NSBackwardsSearch range:candidateRangeBeforeLocation];
NSRange periodAfterRange = [string rangeOfString:@"." options:(NSStringCompareOptions)0 range:candidateRangeAfterLocation];
if (periodBeforeRange.location != NSNotFound)
{
superRange.length -= (periodBeforeRange.location + 1 - superRange.location);
superRange.location = periodBeforeRange.location + 1;
}
if (periodAfterRange.location != NSNotFound)
{
superRange.length -= (NSMaxRange(superRange) - periodAfterRange.location);
}
return superRange;
}
@end
. NSTextView, awakeFromNib; , , , , ; awakeFromNib , , nib, . ( textview - NSTextView):
[[textview layoutManager] replaceTextStorage:[[[MyTextStorage alloc] init] autorelease]];
, !
, , NSAttributedString, nextWordFromIndex:forward:, Cocoa, / . , , . - , / a.b.c.d(, , ) - , . .