123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- #import "CYRLayoutManager.h"
- static CGFloat kMinimumGutterWidth = 30.f;
- @interface CYRLayoutManager ()
- @property (nonatomic, assign) CGFloat gutterWidth;
- @property (nonatomic, assign) UIEdgeInsets lineAreaInset;
- @property (nonatomic) NSUInteger lastParaLocation;
- @property (nonatomic) NSUInteger lastParaNumber;
- @end
- @implementation CYRLayoutManager
- #pragma mark - Initialization & Setup
- - (instancetype)init
- {
- self = [super init];
-
- if (self)
- {
- [self _commonSetup];
- }
-
- return self;
- }
- - (instancetype)initWithCoder:(NSCoder *)aDecoder
- {
- self = [super initWithCoder:aDecoder];
-
- if (self)
- {
- [self _commonSetup];
- }
-
- return self;
- }
- - (void)_commonSetup
- {
- self.gutterWidth = kMinimumGutterWidth;
- self.selectedRange = NSMakeRange(0, 0);
-
- self.lineAreaInset = UIEdgeInsetsMake(0, 10, 0, 4);
- self.lineNumberColor = [UIColor grayColor];
- self.lineNumberFont = [UIFont systemFontOfSize:10.0f];
- }
- #pragma mark - Convenience
- - (CGRect)paragraphRectForRange:(NSRange)range
- {
- range = [self.textStorage.string paragraphRangeForRange:range];
- range = [self glyphRangeForCharacterRange:range actualCharacterRange:NULL];
-
- CGRect startRect = [self lineFragmentRectForGlyphAtIndex:range.location effectiveRange:NULL];
- CGRect endRect = [self lineFragmentRectForGlyphAtIndex:range.location + range.length - 1 effectiveRange:NULL];
-
- CGRect paragraphRectForRange = CGRectUnion(startRect, endRect);
- paragraphRectForRange = CGRectOffset(paragraphRectForRange, _gutterWidth, 8);
-
- return paragraphRectForRange;
- }
- - (NSUInteger) _paraNumberForRange:(NSRange) charRange
- {
-
-
-
-
-
-
-
-
-
-
- if (charRange.location == self.lastParaLocation)
- return self.lastParaNumber;
- else if (charRange.location < self.lastParaLocation)
- {
-
-
-
- NSString* s = self.textStorage.string;
- __block NSUInteger paraNumber = self.lastParaNumber;
-
- [s enumerateSubstringsInRange:NSMakeRange(charRange.location, self.lastParaLocation - charRange.location)
- options:NSStringEnumerationByParagraphs |
- NSStringEnumerationSubstringNotRequired |
- NSStringEnumerationReverse
- usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){
- if (enclosingRange.location <= charRange.location) {
- *stop = YES;
- }
- --paraNumber;
- }];
-
- self.lastParaLocation = charRange.location;
- self.lastParaNumber = paraNumber;
-
- return paraNumber;
- }
- else
- {
-
-
-
- NSString* s = self.textStorage.string;
- __block NSUInteger paraNumber = self.lastParaNumber;
-
- [s enumerateSubstringsInRange:NSMakeRange(self.lastParaLocation, charRange.location - self.lastParaLocation)
- options:NSStringEnumerationByParagraphs | NSStringEnumerationSubstringNotRequired
- usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){
- if (enclosingRange.location >= charRange.location) {
- *stop = YES;
- }
- ++paraNumber;
- }];
-
- self.lastParaLocation = charRange.location;
- self.lastParaNumber = paraNumber;
- return paraNumber;
- }
- }
- #pragma mark - Layouting
- - (void)processEditingForTextStorage:(NSTextStorage *)textStorage edited:(NSTextStorageEditActions)editMask range:(NSRange)newCharRange changeInLength:(NSInteger)delta invalidatedRange:(NSRange)invalidatedCharRange
- {
- [super processEditingForTextStorage:textStorage edited:editMask range:newCharRange changeInLength:delta invalidatedRange:invalidatedCharRange];
-
- if (invalidatedCharRange.location < self.lastParaLocation)
- {
-
-
-
- self.lastParaLocation = 0;
- self.lastParaNumber = 0;
- }
- }
- #pragma mark - Drawing
- - (void) drawBackgroundForGlyphRange:(NSRange)glyphsToShow atPoint:(CGPoint)origin
- {
- [super drawBackgroundForGlyphRange:glyphsToShow atPoint:origin];
-
- NSDictionary* atts = @{NSFontAttributeName : _lineNumberFont ,
- NSForegroundColorAttributeName : _lineNumberColor};
-
- [self enumerateLineFragmentsForGlyphRange:glyphsToShow
- usingBlock:^(CGRect rect, CGRect usedRect, NSTextContainer *textContainer, NSRange glyphRange, BOOL *stop) {
- NSRange charRange = [self characterRangeForGlyphRange:glyphRange actualGlyphRange:nil];
- NSRange paraRange = [self.textStorage.string paragraphRangeForRange:charRange];
-
- BOOL showCursorRect = NSLocationInRange(_selectedRange.location, paraRange);
-
- if (showCursorRect)
- {
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGRect cursorRect = CGRectMake(0, usedRect.origin.y + 8, _gutterWidth, usedRect.size.height);
-
- CGContextSetFillColorWithColor(context, [UIColor colorWithWhite:0.9 alpha:1].CGColor);
- CGContextFillRect(context, cursorRect);
- }
-
-
- if (charRange.location == paraRange.location) {
- CGRect gutterRect = CGRectOffset(CGRectMake(0, rect.origin.y, _gutterWidth, rect.size.height), origin.x, origin.y);
- NSUInteger paraNumber = [self _paraNumberForRange:charRange];
- NSString* ln = [NSString stringWithFormat:@"%ld", (unsigned long) paraNumber + 1];
- CGSize size = [ln sizeWithAttributes:atts];
-
- [ln drawInRect:CGRectOffset(gutterRect, CGRectGetWidth(gutterRect) - _lineAreaInset.right - size.width - _gutterWidth, (CGRectGetHeight(gutterRect) - size.height) / 2.0)
- withAttributes:atts];
- }
- }];
- }
- @end
|