Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Alternate display mode for multi-column text #5

Closed
wants to merge 1 commit into from

2 participants

@benjaminjackson

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.

@benjaminjackson benjaminjackson 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.
99b2c11
@akosma

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

@akosma
Owner

Thanks for the pull request! I've added a comment though, as the variable name attr_string does not really fit the naming conventions of the project. Could you please modify that and resubmit? Thanks!

@akosma akosma closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 28, 2011
  1. @benjaminjackson

    Added texts property to AKOMultiColumnTextView, which takes an array …

    benjaminjackson authored
    …of strings for display in each column rather than flowing a single text through multiple columns.
This page is out of date. Refresh to see the latest.
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
76 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<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,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
Something went wrong with that request. Please try again.