在Three20中,有TTStyledTextLabel類,它相當於一個在IE中顯示一段HTML的顯示窗口,它的實現由兩個部分來組成:
1.創建TTStyledTextLabel對象,並且將它增加到當前的View中。如下圖及代碼。
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 \
<img src=\"bundle://smiley.png\"/>, and <a href=\"http://www.google.com\">hyperlinks</a> you can \
actually touch. URLs are automatically converted into links, like this: http://www.ccwit.com\
<div>You can enclose blocks within an HTML div.</div>\
Both line break characters\n\nand HTML line breaks<br/>are respected.";
TTStyledTextLabel* label1 = [[[TTStyledTextLabelalloc]initWithFrame:self.view.bounds]autorelease];
label1.font = [UIFontsystemFontOfSize:17];
label1.text = [TTStyledTexttextFromXHTML:kText lineBreaks:YESURLs:YES];
label1.contentInset =UIEdgeInsetsMake(10,10, 10, 10);
[label1 sizeToFit];
[self.viewaddSubview:label1];
@interface TextTestStyleSheet :TTDefaultStyleSheet
@end
@implementation TextTestStyleSheet
- (TTStyle*)blueText {
return [TTTextStylestyleWithColor:[UIColorblueColor] next:nil];
}
- (TTStyle*)largeText {
return [TTTextStylestyleWithFont:[UIFontsystemFontOfSize:32]next:nil];
}
- (TTStyle*)smallText {
return [TTTextStylestyleWithFont:[UIFontsystemFontOfSize:12]next:nil];
}
- (TTStyle*)floated {
return [TTBoxStylestyleWithMargin:UIEdgeInsetsMake(0,0, 5,5)
padding:UIEdgeInsetsMake(0,0, 0, 0)
minSize:CGSizeZeroposition:TTPositionFloatLeftnext:nil];
}
- (TTStyle*)blueBox {
return
[TTShapeStyle styleWithShape:[TTRoundedRectangleShapeshapeWithRadius:6]next:
[TTInsetStylestyleWithInset:UIEdgeInsetsMake(0, -5, -4, -6) next:
[TTShadowStylestyleWithColor:[UIColorgrayColor] blur:2offset:CGSizeMake(1,1)next:
[TTSolidFillStylestyleWithColor:[UIColorpurpleColor] next:
[TTSolidBorderStylestyleWithColor:[UIColorgrayColor] width:1next:nil]]]]];
}
- (TTStyle*)inlineBox {
return
[TTSolidFillStylestyleWithColor:[UIColorblueColor] next:
[TTBoxStylestyleWithPadding:UIEdgeInsetsMake(5,13,5,13)next:
[TTSolidBorderStylestyleWithColor:[UIColorblackColor] width:1next:nil]]];
}
- (TTStyle*)inlineBox2 {
return
[TTSolidFillStylestyleWithColor:[UIColorcyanColor] next:
[TTBoxStylestyleWithMargin:UIEdgeInsetsMake(5,50,0,50)
padding:UIEdgeInsetsMake(0,13,0,13)next:nil]];
}
@end
[TTStyleSheetsetGlobalStyleSheet:[[[TextTestStyleSheetalloc] init]autorelease]];
使label顯示時使用TextTestStyleSheet提供的風格來顯示。如下圖所示的效果: