Skip to content
This repository has been archived by the owner on Oct 5, 2018. It is now read-only.

Alternate display mode for multi-column text #5

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions Classes/Views/AKOMultiColumnTextView.h
Expand Up @@ -16,7 +16,9 @@
NSInteger _columnCount;
UIFont *_font;
NSString *_text;
NSArray *_texts;
NSMutableAttributedString *_attributedString;
NSMutableArray *_attributedStrings;
UIColor *_color;
CFMutableArrayRef _columnPaths;
CFMutableArrayRef _frames;
Expand All @@ -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;
Expand Down
76 changes: 67 additions & 9 deletions Classes/Views/AKOMultiColumnTextView.m
@@ -1,4 +1,5 @@


// AKOMultiColumnTextView.m
// CoreTextWrapper
//
Expand All @@ -13,7 +14,7 @@ @interface AKOMultiColumnTextView ()

@property (nonatomic, retain) NSMutableAttributedString *attributedString;

- (void)updateAttributedString;
- (NSMutableAttributedString *)newAttributedStringWithString:(NSString *)aString;
- (void)updateFrames;
- (void)setup;
- (void)createColumns;
Expand All @@ -26,6 +27,7 @@ @implementation AKOMultiColumnTextView
@dynamic font;
@dynamic columnCount;
@dynamic text;
@dynamic texts;
@dynamic color;
@synthesize startIndex = _startIndex;
@synthesize finalIndex = _finalIndex;
Expand All @@ -47,6 +49,7 @@ @implementation AKOMultiColumnTextView

- (void)setup
{
_attributedStrings = [[NSMutableArray alloc] init];
self.backgroundColor = [UIColor whiteColor];
_columnCount = 3;
_text = [@"" copy];
Expand Down Expand Up @@ -101,6 +104,8 @@ - (void)dealloc
self.dataSource = nil;
self.attributedString = nil;

[_attributedStrings release];
_attributedStrings = nil;
[_text release];
_text = nil;
[_font release];
Expand Down Expand Up @@ -147,6 +152,11 @@ - (NSString *)text
return _text;
}

- (NSArray *)texts
{
return _texts;
}

- (void)setDataSource:(id<AKOMultiColumnTextViewDataSource>)dataSource
{
if (![_dataSource isEqual:dataSource])
Expand All @@ -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;
Expand Down Expand Up @@ -322,25 +346,26 @@ - (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);
}

if (self.color != nil)
{
[self.attributedString addAttribute:(NSString *)kCTForegroundColorAttributeName
[attr_string addAttribute:(NSString *)kCTForegroundColorAttributeName
value:(id)self.color.CGColor
range:range];
}
Expand All @@ -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];

Expand Down Expand Up @@ -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
Expand Down