Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Allow setting of text by using an array #7

Merged
merged 2 commits into from

2 participants

@orta

Hello there, I've cleaned up this pull request: #5 and made it fit your style, kudos of course goes mainly to @benjaminjackson

I've been using this for a while so thought I'd also say thank you!

@akosma akosma merged commit e77166f into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
2  Classes/Controllers/PageController.m
@@ -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;
View
3  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;
View
113 Classes/Views/AKOMultiColumnTextView.m
@@ -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,22 @@ - (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 +391,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 +472,44 @@ - (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
Something went wrong with that request. Please try again.