1.引言
今天剛接觸到three320 主要是遇到一個難題就是大量的文字在lable 上顯示的時候,顯示不同的字體,簡單的方法採用兩個lable 來顯示,但數據一但很多的時候或則交錯的時候是行不通的,所以纔想別的方法。下面談談自己一個下午的初體會
2.three 20 是什麼
three20 是從第三方facebook 剝離出的一個框架, 主要涉及三大模塊,網絡 ,stysle 和ui
突破了原有的模式,可以方便的定製出適合的界面風格,實現很多神奇的效果
如果想模仿iPhone郵件應用中的郵件編輯器,可以使用TTMessageController組件;如果想採用像Facebook那樣的消息發送窗口,可以使用TTPostController組件。
還有支持Safari和YouTube的視圖控制器。
Three20有一個功能強大的表格結構:單元格/條目結構。它在標準的蘋果單元格類型基礎上添加了幾種特殊的單元格類型。通過它可以方便地創建一些包含內嵌控件的表格視圖控制器。例如:
如果想在應用程序中顯示一個更小的活動指示器,可以使用Three20中的活動標籤,它提供了指示器+文本+進度條的顯示方式。
使用TTLauncherView組件來模擬iPhone的桌面。
支持在頂部的分頁標籤。
樣式(Style)
Three20的樣式組件能非常方便的聲明和複用。通過自定義樣式表, 可以用幾個簡單的聲明就可以創建可以圓角的按鈕、陰影、漸變和邊框。下面是幾個樣式表元素的例子:
上面的這些例子在官方的文檔裏都有具體的可實現的例子,而且對各個類和模塊怎樣使用都有很好的說明。引用這樣的一個第三庫可以節約很多時間。
3怎樣添加這個第三庫到xcode 四中去
在官方文檔裏面有很詳細的英文教程,網上有很多關於怎樣添加到xcode3.2 版本的教程,xcode4和前一個版本變化真的很大,那些教程摸索了好久也沒找到對應的項,只好看英文版的。
添加到第三方庫到以存在的工程有兩種方式
(1)使用命令
(2) 手動加載
手動的沒怎麼看明白,感覺還是命令的方式比較好,相當的簡單
3.1 下載源代碼
https://github.com/facebook/three20/archives/master
(下載好源代碼後把文件最好解壓到你的現有工程的文件目錄下)
3.2打開mac 的終端
輸入以下命令(最好用命令切換到工程目錄,命令裏面的路徑改下,改成對應的目錄)
- python three20/src/scripts/ttmodule.py -p path/to/your/project/project.xcodeproj Three20 --xcode-version=4
3,3打開目錄就可以看到工程加載到你的工程目錄下了
在你的工程的prefix-pch 中添加 #import <Three20/Three20+Additions.h>
這樣每個類都可以使用這格第三方庫文件了()
在文件種添加下面幾個簡單的代碼
- @implementation TextTestStyleSheet
- - (TTStyle*)blueText {
- return [TTTextStyle styleWithColor:[UIColor blueColor] next:nil];
- }
- - (TTStyle*)largeText {
- return [TTTextStyle styleWithFont:[UIFont systemFontOfSize:32] next:nil];
- }
- - (TTStyle*)smallText {
- return [TTTextStyle styleWithFont:[UIFont systemFontOfSize:12] next:nil];
- }
- - (TTStyle*)floated {
- return [TTBoxStyle styleWithMargin:UIEdgeInsetsMake(0, 0, 5, 5)
- padding:UIEdgeInsetsMake(0, 0, 0, 0)
- minSize:CGSizeZero position:TTPositionFloatLeft next:nil];
- }
- - (TTStyle*)blueBox {
- return
- [TTShapeStyle styleWithShape:[TTRoundedRectangleShape shapeWithRadius:6] next:
- [TTInsetStyle styleWithInset:UIEdgeInsetsMake(0, -5, -4, -6) next:
- [TTShadowStyle styleWithColor:[UIColor grayColor] blur:2 offset:CGSizeMake(1,1) next:
- [TTSolidFillStyle styleWithColor:[UIColor cyanColor] next:
- [TTSolidBorderStyle styleWithColor:[UIColor grayColor] width:1 next:nil]]]]];
- }
- - (TTStyle*)inlineBox {
- return
- [TTSolidFillStyle styleWithColor:[UIColor blueColor] 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 styleWithMargin:UIEdgeInsetsMake(5,50,0,50)
- padding:UIEdgeInsetsMake(0,13,0,13) next:nil]];
- }
- @end
- 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 p_w_picpaths \
- <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.foo.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 = [[[TTStyledTextLabel alloc] initWithFrame:CGRectMake(0, 10, 320, 480)] autorelease];
- label1.font = [UIFont systemFontOfSize:17];
- label1.text = [TTStyledText textFromXHTML:kText lineBreaks:YES URLs:YES];
- label1.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
- //label1.backgroundColor = [UIColor grayColor];
- [label1 sizeToFit];
- [self.view addSubview:label1];}
效果如下
初步研究,歡迎指正。