Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Commit

Permalink
* Support padding on block elements
Browse files Browse the repository at this point in the history
Signed-off-by: Joakim Bodin <joakim.bodin@gmail.com>
  • Loading branch information
joehewitt authored and Joakim Bodin committed Apr 20, 2009
1 parent df87d60 commit 2996ce5
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 16 deletions.
11 changes: 7 additions & 4 deletions samples/TTCatalog/Classes/StyledTextTestController.m
Expand Up @@ -25,14 +25,14 @@ - (TTStyle*)blueBox {
- (TTStyle*)inlineBox {
return
[TTSolidFillStyle styleWithColor:[UIColor blueColor] next:
[TTBoxStyle styleWithPadding:UIEdgeInsetsMake(5,13,2,13) next:
[TTBoxStyle styleWithPadding:UIEdgeInsetsMake(5,13,5,13) next:
[TTSolidBorderStyle styleWithColor:[UIColor blackColor] width:1 next:nil]]];
}

- (TTStyle*)inlineBox2 {
return
[TTSolidFillStyle styleWithColor:[UIColor cyanColor] next:
[TTBoxStyle styleWithPadding:UIEdgeInsetsMake(2,13,5,13) next:nil]];
[TTBoxStyle styleWithPadding:UIEdgeInsetsMake(0,13,0,13) next:nil]];
}

@end
Expand Down Expand Up @@ -60,7 +60,8 @@ - (void)dealloc {
- (void)loadView {
[super loadView];

NSString* kText = @"This is a test of styled labels. Styled labels support \
NSString* kText = @"\
This is a test of styled labels. Styled labels support \
<b>bold text</b>, <i>italic text</i>, <span class=\"blueText\">colored text</span>, \
<span class=\"largeText\">font sizes</span>, \
<span class=\"blueBox\">spans with backgrounds</span>, inline images \
Expand All @@ -72,9 +73,11 @@ - (void)loadView {
// NSString* kText = @"blah blah blah black sheep blah <span class=\"inlineBox\">\
//<img src=\"bundle://smiley.png\"/>hyperlinks</span> blah fun";
// NSString* kText = @"\
//<div class=\"inlineBox\">You can enclose blocks within an HTML div.</div>";
//<div class=\"inlineBox\"><div class=\"inlineBox2\">You can enclose blocks within an HTML div.</div></div>";
// NSString* kText = @"\
//<span class=\"inlineBox\"><span class=\"inlineBox2\">You can enclose blocks within an HTML div.</span></span>";
// NSString* kText = @"\
//<div class=\"inlineBox2\">You can enclose blocks within an HTML div.</div>test";

TTStyledTextLabel* label1 = [[[TTStyledTextLabel alloc] initWithFrame:self.view.bounds] autorelease];
label1.font = [UIFont systemFontOfSize:17];
Expand Down
30 changes: 21 additions & 9 deletions src/TTStyledLayout.m
Expand Up @@ -165,7 +165,7 @@ - (void)breakLine {
_height += _lineHeight;
_lineWidth = 0;
_lineHeight = 0;
_x = 0;
_x = _minX;
_lineFirstFrame = nil;

if (_inlineFrame) {
Expand Down Expand Up @@ -221,9 +221,10 @@ - (void)layoutElement:(TTStyledElement*)elt {

TTStyledFrame* blockFrame = nil;
BOOL isBlock = [elt isKindOfClass:[TTStyledBlock class]];

if (isBlock) {
if (_lastFrame) {
if (!_lineHeight) {
if (!_lineHeight && [elt isKindOfClass:[TTStyledLineBreakNode class]]) {
_lineHeight = self.fontHeight;
}
[self breakLine];
Expand All @@ -237,10 +238,17 @@ - (void)layoutElement:(TTStyledElement*)elt {
}
}

CGFloat minX = _minX;
CGFloat maxWidth = _maxWidth;

TTBoxStyle* padding = nil;
if (style) {
padding = [style firstStyleOfClass:[TTBoxStyle class]];
if (padding) {
if (isBlock) {
_minX += padding.padding.left;
}
_maxWidth -= padding.padding.left+padding.padding.right;
_x += padding.padding.left;
_lineWidth += padding.padding.left;

Expand Down Expand Up @@ -268,15 +276,18 @@ - (void)layoutElement:(TTStyledElement*)elt {
_font = lastFont;
_lastStyle = lastStyle;

if (isBlock) {
[self breakLine];
}
}

if (isBlock && style) {
_x += padding.padding.right;
_lineWidth += padding.padding.right;
_height += padding.padding.top+padding.padding.bottom;
if (isBlock) {
_minX = minX;
_maxWidth = maxWidth;
[self breakLine];

if (padding) {
// _x += padding.padding.right;
// _lineWidth += padding.padding.right;
_height += padding.padding.bottom;
}
blockFrame.height = _height - blockFrame.height;
} else if (!isBlock && style) {
_inlineFrame.height += self.fontHeight;
Expand Down Expand Up @@ -416,6 +427,7 @@ - (id)init {
_height = 0;
_lineWidth = 0;
_lineHeight = 0;
_minX = 0;
_maxWidth = 0;
_styleStack = nil;
_lastStyle = nil;
Expand Down
5 changes: 5 additions & 0 deletions src/TTStyledNode.m
Expand Up @@ -367,3 +367,8 @@ - (NSString*)description {
}

@end

//////////////////////////////////////////////////////////////////////////////////////////////////

@implementation TTStyledLineBreakNode
@end
9 changes: 6 additions & 3 deletions src/TTStyledTextParser.m
Expand Up @@ -57,7 +57,7 @@ - (void)flushCharacters {
[self parseURLs:substr];

// Add a line break node after the text
TTStyledBlock* br = [[[TTStyledBlock alloc] init] autorelease];
TTStyledLineBreakNode* br = [[[TTStyledLineBreakNode alloc] init] autorelease];
[self addNode:br];

index = index + substr.length + 1;
Expand Down Expand Up @@ -116,8 +116,11 @@ - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
TTStyledInline* node = [[[TTStyledInline alloc] init] autorelease];
node.className = [attributeDict objectForKey:@"class"];
[self pushNode:node];
} else if ([tag isEqualToString:@"div"] || [tag isEqualToString:@"p"]
|| [tag isEqualToString:@"br"]) {
} else if ([tag isEqualToString:@"br"]) {
TTStyledLineBreakNode* node = [[[TTStyledLineBreakNode alloc] init] autorelease];
node.className = [attributeDict objectForKey:@"class"];
[self pushNode:node];
} else if ([tag isEqualToString:@"div"] || [tag isEqualToString:@"p"]) {
TTStyledBlock* node = [[[TTStyledBlock alloc] init] autorelease];
node.className = [attributeDict objectForKey:@"class"];
[self pushNode:node];
Expand Down
1 change: 1 addition & 0 deletions src/Three20/TTStyledLayout.h
Expand Up @@ -8,6 +8,7 @@
CGFloat _height;
CGFloat _lineWidth;
CGFloat _lineHeight;
CGFloat _minX;
CGFloat _maxWidth;
NSMutableArray* _styleStack;
TTStyle* _lastStyle;
Expand Down
5 changes: 5 additions & 0 deletions src/Three20/TTStyledNode.h
Expand Up @@ -98,3 +98,8 @@
- (id)initWithURL:(NSString*)url;

@end

///////////////////////////////////////////////////////////////////////////////////////////////////

@interface TTStyledLineBreakNode : TTStyledBlock
@end

0 comments on commit 2996ce5

Please sign in to comment.