From 99b2c11c020e36eb7294444fadcf295a2f992ec7 Mon Sep 17 00:00:00 2001 From: Benjamin Jackson Date: Thu, 27 Oct 2011 20:35:32 -0400 Subject: [PATCH] Added texts property to AKOMultiColumnTextView, which takes an array of strings for display in each column rather than flowing a single text through multiple columns. --- Classes/Views/AKOMultiColumnTextView.h | 3 + Classes/Views/AKOMultiColumnTextView.m | 76 +++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 9 deletions(-) diff --git a/Classes/Views/AKOMultiColumnTextView.h b/Classes/Views/AKOMultiColumnTextView.h index 1e52ba6..50cfcc7 100644 --- a/Classes/Views/AKOMultiColumnTextView.h +++ b/Classes/Views/AKOMultiColumnTextView.h @@ -16,7 +16,9 @@ NSInteger _columnCount; UIFont *_font; NSString *_text; + NSArray *_texts; NSMutableAttributedString *_attributedString; + NSMutableArray *_attributedStrings; UIColor *_color; CFMutableArrayRef _columnPaths; CFMutableArrayRef _frames; @@ -39,6 +41,7 @@ @property (nonatomic) NSInteger columnCount; @property (nonatomic, retain) UIFont *font; @property (nonatomic, copy) NSString *text; +@property (nonatomic, copy) NSArray *texts; @property (nonatomic, retain) UIColor *color; @property (nonatomic) CFIndex startIndex; @property (nonatomic, readonly) CFIndex finalIndex; diff --git a/Classes/Views/AKOMultiColumnTextView.m b/Classes/Views/AKOMultiColumnTextView.m index 56d88e5..5d890aa 100644 --- a/Classes/Views/AKOMultiColumnTextView.m +++ b/Classes/Views/AKOMultiColumnTextView.m @@ -1,4 +1,5 @@ + // AKOMultiColumnTextView.m // CoreTextWrapper // @@ -13,7 +14,7 @@ @interface AKOMultiColumnTextView () @property (nonatomic, retain) NSMutableAttributedString *attributedString; -- (void)updateAttributedString; +- (NSMutableAttributedString *)newAttributedStringWithString:(NSString *)aString; - (void)updateFrames; - (void)setup; - (void)createColumns; @@ -26,6 +27,7 @@ @implementation AKOMultiColumnTextView @dynamic font; @dynamic columnCount; @dynamic text; +@dynamic texts; @dynamic color; @synthesize startIndex = _startIndex; @synthesize finalIndex = _finalIndex; @@ -47,6 +49,7 @@ @implementation AKOMultiColumnTextView - (void)setup { + _attributedStrings = [[NSMutableArray alloc] init]; self.backgroundColor = [UIColor whiteColor]; _columnCount = 3; _text = [@"" copy]; @@ -101,6 +104,8 @@ - (void)dealloc self.dataSource = nil; self.attributedString = nil; + [_attributedStrings release]; + _attributedStrings = nil; [_text release]; _text = nil; [_font release]; @@ -147,6 +152,11 @@ - (NSString *)text return _text; } +- (NSArray *)texts +{ + return _texts; +} + - (void)setDataSource:(id)dataSource { if (![_dataSource isEqual:dataSource]) @@ -173,6 +183,20 @@ - (void)setText:(NSString *)newText } } +- (void)setTexts:(NSArray *)newTexts { + if (![_texts isEqual:newTexts]) { + [_text release]; + _text = nil; + + [_texts release]; + _texts = [newTexts copy]; + + self.attributedString = nil; + [self updateFrames]; + [self setNeedsDisplay]; + } +} + - (NSInteger)columnCount { return _columnCount; @@ -322,17 +346,18 @@ - (void)createColumns } -- (void)updateAttributedString +- (NSMutableAttributedString *)newAttributedStringWithString:(NSString *)aString { - if (self.text != nil) + NSMutableAttributedString *attr_string = nil; + if (aString != nil) { - self.attributedString = [[[NSMutableAttributedString alloc] initWithString:self.text] autorelease]; - NSRange range = NSMakeRange(0, [self.text length]); + attr_string = [[NSMutableAttributedString alloc] initWithString:aString]; + NSRange range = NSMakeRange(0, [aString length]); if (self.font != nil) { CTFontRef font = self.font.CTFont; - [self.attributedString addAttribute:(NSString *)kCTFontAttributeName + [attr_string addAttribute:(NSString *)kCTFontAttributeName value:(id)font range:range]; CFRelease(font); @@ -340,7 +365,7 @@ - (void)updateAttributedString if (self.color != nil) { - [self.attributedString addAttribute:(NSString *)kCTForegroundColorAttributeName + [attr_string addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)self.color.CGColor range:range]; } @@ -358,19 +383,20 @@ - (void)updateAttributedString }; CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(theSettings, theNumberOfSettings); - [self.attributedString addAttribute:(NSString *)kCTParagraphStyleAttributeName + [attr_string addAttribute:(NSString *)kCTParagraphStyleAttributeName value:(id)paragraphStyle range:range]; CFRelease(paragraphStyle); } + return attr_string; } - (void)updateFrames { if (self.text != nil) { - [self updateAttributedString]; + _attributedString = [self newAttributedStringWithString:self.text]; CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)self.attributedString); [self createColumns]; @@ -399,6 +425,38 @@ - (void)updateFrames _moreTextAvailable = [self.text length] > self.finalIndex; CFRelease(framesetter); } + else if (self.texts != nil) { + [_attributedStrings removeAllObjects]; + for (int column = 0; column < _columnCount; column++) { + if (column < [_texts count]) { + NSString *currentText = [_texts objectAtIndex:column]; + [_attributedStrings addObject:[[self newAttributedStringWithString:currentText] autorelease]]; + } + } + [self createColumns]; + CFIndex pathCount = CFArrayGetCount(_columnPaths); + + if (_frames != NULL) + { + CFRelease(_frames); + } + _frames = CFArrayCreateMutable(kCFAllocatorDefault, pathCount, &kCFTypeArrayCallBacks); + + for (int column = 0; column < pathCount; column++) + { + CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)[_attributedStrings objectAtIndex:column]); + + CGPathRef path = (CGPathRef)CFArrayGetValueAtIndex(_columnPaths, column); + + CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL); + CFArrayInsertValueAtIndex(_frames, column, frame); + + CFRelease(frame); + CFRelease(framesetter); + } + + _moreTextAvailable = NO; + } } - (void)setPage:(NSInteger)page