Skip to content

Commit

Permalink
Merge pull request facebookarchive#467 from rogchap/fix-ttsyledtextla…
Browse files Browse the repository at this point in the history
…bel-alignment

Implemented support for UITextAlignment to TTStyledTextLabel - closes #101
  • Loading branch information
jwang committed Jul 10, 2011
2 parents 48af7a1 + 15311f7 commit c78fc97
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions samples/TTCatalog/Classes/StyledTextTestController.m
Expand Up @@ -114,6 +114,7 @@ - (void)loadView {
label1.text = [TTStyledText textFromXHTML:kText lineBreaks:YES URLs:YES];
label1.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
//label1.backgroundColor = [UIColor grayColor];
//label1.textAlignment = UITextAlignmentCenter;
[label1 sizeToFit];
[self.view addSubview:label1];
}
Expand Down
3 changes: 3 additions & 0 deletions src/Three20Style/Headers/TTStyledLayout.h
Expand Up @@ -45,6 +45,8 @@
UIFont* _boldFont;
UIFont* _italicFont;

UITextAlignment _textAlignment;

TTStyle* _linkStyle;
TTStyledNode* _rootNode;
TTStyledNode* _lastNode;
Expand All @@ -55,6 +57,7 @@
@property (nonatomic) CGFloat width;
@property (nonatomic) CGFloat height;
@property (nonatomic, retain) UIFont* font;
@property (nonatomic) UITextAlignment textAlignment;
@property (nonatomic, readonly) TTStyledFrame* rootFrame;
@property (nonatomic, retain) NSMutableArray* invalidImages;

Expand Down
6 changes: 6 additions & 0 deletions src/Three20Style/Headers/TTStyledText.h
Expand Up @@ -28,6 +28,7 @@
TTStyledNode* _rootNode;
TTStyledFrame* _rootFrame;
UIFont* _font;
UITextAlignment _textAlignment;
CGFloat _width;
CGFloat _height;
NSMutableArray* _invalidImages;
Expand All @@ -53,6 +54,11 @@
*/
@property (nonatomic, retain) UIFont* font;

/**
* The text alignment used for all text.
*/
@property (nonatomic) UITextAlignment textAlignment;

/**
* The width that the text should be constrained to fit within.
*/
Expand Down
27 changes: 27 additions & 0 deletions src/Three20Style/Sources/TTStyledLayout.m
Expand Up @@ -51,6 +51,7 @@ @implementation TTStyledLayout
@synthesize height = _height;
@synthesize rootFrame = _rootFrame;
@synthesize font = _font;
@synthesize textAlignment = _textAlignment;
@synthesize invalidImages = _invalidImages;


Expand Down Expand Up @@ -177,6 +178,20 @@ - (void)offsetFrame:(TTStyledFrame*)frame by:(CGFloat)y {
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)offsetFrame:(TTStyledFrame*)frame byX:(CGFloat)x {
frame.x += x;

if ([frame isKindOfClass:[TTStyledInlineFrame class]]) {
TTStyledInlineFrame* inlineFrame = (TTStyledInlineFrame*)frame;
TTStyledFrame* child = inlineFrame.firstChildFrame;
while (child) {
[self offsetFrame:child byX:x];
child = child.nextFrame;
}
}
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)expandLineWidth:(CGFloat)width {
Expand Down Expand Up @@ -366,6 +381,18 @@ - (void)breakLine {
}
}

// Horizontally align all frames on current line if required
if (_textAlignment != UITextAlignmentLeft) {
CGFloat remainingSpace = _width - _lineWidth;
CGFloat offset = _textAlignment == UITextAlignmentCenter ? remainingSpace/2 : remainingSpace;

TTStyledFrame* frame = _lineFirstFrame;
while (frame) {
[self offsetFrame:frame byX:offset];
frame = frame.nextFrame;
}
}

_height += _lineHeight;
[self checkFloats];

Expand Down
10 changes: 10 additions & 0 deletions src/Three20Style/Sources/TTStyledText.m
Expand Up @@ -57,6 +57,7 @@ @implementation TTStyledText

@synthesize rootNode = _rootNode;
@synthesize font = _font;
@synthesize textAlignment = _textAlignment;
@synthesize width = _width;
@synthesize height = _height;
@synthesize invalidImages = _invalidImages;
Expand Down Expand Up @@ -299,6 +300,14 @@ - (void)setFont:(UIFont*)font {
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setTextAlignment:(UITextAlignment)textAlignment {
if (textAlignment != _textAlignment) {
_textAlignment = textAlignment;
[self setNeedsLayout];
}
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setWidth:(CGFloat)width {
Expand Down Expand Up @@ -327,6 +336,7 @@ - (void)layoutFrames {
TTStyledLayout* layout = [[TTStyledLayout alloc] initWithRootNode:_rootNode];
layout.width = _width;
layout.font = _font;
layout.textAlignment = _textAlignment;
[layout layout:_rootNode];

[_rootFrame release];
Expand Down
2 changes: 1 addition & 1 deletion src/Three20UI/Headers/TTStyledTextLabel.h
Expand Up @@ -68,7 +68,7 @@
@property (nonatomic, retain) UIColor* highlightedTextColor;

/**
* The alignment of the text. (NOT YET IMPLEMENTED)
* The alignment of the text.
*/
@property (nonatomic) UITextAlignment textAlignment;

Expand Down
9 changes: 9 additions & 0 deletions src/Three20UI/Sources/TTStyledTextLabel.m
Expand Up @@ -454,6 +454,7 @@ - (void)setText:(TTStyledText*)text {
_text = [text retain];
_text.delegate = self;
_text.font = _font;
_text.textAlignment = _textAlignment;
[self setNeedsLayout];
[self setNeedsDisplay];
}
Expand Down Expand Up @@ -482,6 +483,14 @@ - (void)setFont:(UIFont*)font {
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setTextAlignment:(UITextAlignment)textAlignment {
if (textAlignment != _textAlignment) {
_textAlignment = textAlignment;
_text.textAlignment = _textAlignment;
[self setNeedsLayout];
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
- (UIColor*)textColor {
Expand Down

0 comments on commit c78fc97

Please sign in to comment.