Permalink
Browse files

added texts to multicolumntextview based of ben jackson's work

  • Loading branch information...
orta committed Jun 25, 2012
1 parent a9a9aa4 commit b3ee9143202806faf2bffdb416561fa2a50adab0
@@ -48,7 +48,7 @@ - (void)viewDidLoad
self.fontSize = 24.0;
self.multiPageView.dataSource = self;
- self.multiPageView.columnInset = CGPointMake(50, 30);
+ self.multiPageView.columnInset = CGPointMake(50, 30);
self.multiPageView.text = [NSString stringFromFileNamed:@"lorem_ipsum.txt"];
self.multiPageView.font = [UIFont fontWithName:@"Georgia" size:self.fontSize];
self.multiPageView.columnCount = UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? 2 : 3;
@@ -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;
@@ -12,6 +12,7 @@
@interface AKOMultiColumnTextView ()
@property (nonatomic, retain) NSMutableAttributedString *attributedString;
+@property (nonatomic, retain) NSMutableArray *attributedStrings;
- (void)updateAttributedString;
- (void)updateFrames;
@@ -26,11 +27,13 @@ @implementation AKOMultiColumnTextView
@dynamic font;
@dynamic columnCount;
@dynamic text;
+@dynamic texts;
@dynamic color;
@synthesize startIndex = _startIndex;
@synthesize finalIndex = _finalIndex;
@synthesize moreTextAvailable = _moreTextAvailable;
@synthesize attributedString = _attributedString;
+@synthesize attributedStrings = _attributedStrings;
@synthesize lineBreakMode = _lineBreakMode;
@synthesize textAlignment = _textAlignment;
@@ -58,7 +61,7 @@ - (void)setup
_columnPaths = NULL;
_frames = NULL;
-
+ _attributedStrings = [[NSMutableArray alloc] init];
_lineBreakMode = kCTLineBreakByWordWrapping;
_textAlignment = kCTLeftTextAlignment;
_firstLineHeadIndent = 0.0;
@@ -100,6 +103,7 @@ - (void)dealloc
self.dataSource = nil;
self.attributedString = nil;
+ self.attributedStrings = nil;
[_text release];
_text = nil;
@@ -147,6 +151,11 @@ - (NSString *)text
return _text;
}
+- (NSArray *)texts
+{
+ return _texts;
+}
+
- (void)setDataSource:(id<AKOMultiColumnTextViewDataSource>)dataSource
{
if (![_dataSource isEqual:dataSource])
@@ -173,6 +182,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;
@@ -366,6 +389,54 @@ - (void)updateAttributedString
}
}
+
+- (NSMutableAttributedString *)newAttributedStringWithString:(NSString *)aString
+{
+ NSMutableAttributedString *attributedString = nil;
+ if (aString != nil)
+ {
+ attributedString = [[NSMutableAttributedString alloc] initWithString:aString];
+ NSRange range = NSMakeRange(0, [aString length]);
+
+ if (self.font != nil)
+ {
+ CTFontRef font = [self.font createCTFont];
+ [attributedString addAttribute:(NSString *)kCTFontAttributeName
+ value:(id)font
+ range:range];
+ CFRelease(font);
+ }
+
+ if (self.color != nil)
+ {
+ [attributedString addAttribute:(NSString *)kCTForegroundColorAttributeName
+ value:(id)self.color.CGColor
+ range:range];
+ }
+
+
+ CFIndex theNumberOfSettings = 6;
+ CTParagraphStyleSetting theSettings[6] =
+ {
+ { kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &_textAlignment },
+ { kCTParagraphStyleSpecifierLineBreakMode, sizeof(CTLineBreakMode), &_lineBreakMode },
+ { kCTParagraphStyleSpecifierFirstLineHeadIndent, sizeof(CGFloat), &_firstLineHeadIndent },
+ { kCTParagraphStyleSpecifierParagraphSpacing, sizeof(CGFloat), &_spacing },
+ { kCTParagraphStyleSpecifierParagraphSpacingBefore, sizeof(CGFloat), &_topSpacing },
+ { kCTParagraphStyleSpecifierLineSpacing, sizeof(CGFloat), &_lineSpacing }
+ };
+
+ CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(theSettings, theNumberOfSettings);
+ [attributedString addAttribute:(NSString *)kCTParagraphStyleAttributeName
+ value:(id)paragraphStyle
+ range:range];
+
+ CFRelease(paragraphStyle);
+ }
+ return attributedString;
+}
+
+
- (void)updateFrames
{
if (self.text != nil)
@@ -399,6 +470,43 @@ - (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++)
+ {
+ if (column < [_attributedStrings count])
+ {
+ 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 b3ee914

Please sign in to comment.