Three20中的TTStyledTextLabel是什麼?

在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];


2.label1對象中的顯示text是通過style來進行控制的,也可以通過類來提供,如下:

@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


最後,別忘在在label1對象有效之前,調用

[TTStyleSheetsetGlobalStyleSheet:[[[TextTestStyleSheetalloc] init]autorelease]];

使label顯示時使用TextTestStyleSheet提供的風格來顯示。如下圖所示的效果:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章