Permalink
Browse files

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.
  • Loading branch information...
1 parent 09930d8 commit 99b2c11c020e36eb7294444fadcf295a2f992ec7 @benjaminjackson committed Oct 28, 2011
Showing with 70 additions and 9 deletions.
  1. +3 −0 Classes/Views/AKOMultiColumnTextView.h
  2. +67 −9 Classes/Views/AKOMultiColumnTextView.m
@@ -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;
@@ -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<AKOMultiColumnTextViewDataSource>)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,25 +346,26 @@ - (void)createColumns
}
-- (void)updateAttributedString
+- (NSMutableAttributedString *)newAttributedStringWithString:(NSString *)aString
{
- if (self.text != nil)
+ NSMutableAttributedString *attr_string = nil;
akosma
akosma Mar 1, 2012

The variable name "attr_string" does not follow the coding conventions of this project (using camelCase variable names)

+ 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);
}
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

0 comments on commit 99b2c11

Please sign in to comment.