IOS控制項Label(UILabel)

來源:互聯網
上載者:User

前段時間已經把 Object-C 過了一遍了,現在要開始 IOS 開發的實戰學習了。因為之前是做 .Net 開發的,所以,轉過來的時間,還是有好多不適應和困惑的,特別是 C# -> Object-C 和  VS -> XCode,其中更有很多思想和操作都是不同的,沒辦法,只有一步步來了。

 今天在調一些IOS中簡單的控制項,其中使用到了 Label(UILabel),其實 Label(UILabel) 還是蠻簡單的,但是也是最常用的,所以就想先通過 深入瞭解 Label(UILabel) ,來開始接觸 IOS 中的控制項,畢竟控制項有好多的屬性和方法都是相同的~ 官方文檔:Apple  Label(UILabel)  官方文檔中,對 Label(UILabel) 描述比較重要的部分如下: The UILabel class implements a read-only text view. You can use this class to draw one or multiple lines of static text, such as those you might use to identify other parts of your user interface. The base UILabel class provides control over the appearance of your text, including whether it uses a shadow or draws with a highlight. If needed, you can customize the appearance of your text further by subclassing.  New label objects are configured to disregard user events by default. If you want to handle events in a custom subclass of UILabel, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object.  本文禁止任何網站轉載,嚴厲譴責那些蛀蟲們。
本文首發於,部落格園,請搜尋:部落格園 - 尋自己,查看原版文章
本文首發地址:IOS控制項Label(UILabel) - www.cnblogs.com/xunziji/archive/2012/09/20/2695474.html 通過這兩段話,我們能明白 Label(UILabel) 主要有以下特性: 1. 是用來顯示常值內容的,且是 唯讀 的 2. 可以單行顯示,也可以多行顯示,但是要通過設定實現 3. 可以給內容增加一些 shadow、draws、highlight 等特效 4. 預設是不支援事件的,但是可以通過自訂使其支援   本文禁止任何網站轉載,嚴厲譴責那些蛀蟲們。
本文首發於,部落格園,請搜尋:部落格園 - 尋自己,查看原版文章
本文首發地址:IOS控制項Label(UILabel) - www.cnblogs.com/xunziji/archive/2012/09/20/2695474.html 下面,來逐條分析下 Label(UILabel)  的屬性(Property):   1. text : Label(UILabel) 顯示的文本, 可讀寫的屬性,實值型別為 NSString,預設值為 Nil     

 2. font : text 的 font-family 和 font-size,可讀寫,實值型別為 UIFont,預設值為 system font at a size of 17 

  3. textColor :  文本的顏色, 可讀寫的屬性,實值型別為 UIColor,預設值為 Black 
   4. textAlignment : 文本的對齊, 可讀寫的屬性,實值型別為 UITextAlignment,預設值為 UITextAlignmentLeft 

  5. lineBreakMode :  文本的換行與截斷方式, 可讀寫的屬性,實值型別為 UILineBreakMode,預設值為 UILineBreakModeTailTruncation   6. enabled :   控制項是否可用,感覺對於 Label 來說比較雞肋,為 false 後,只是把顏色變灰而已,如果想要隱藏UILabel(Label),請用hidden 屬性 
//隱藏Label(UILabel):lblUserName[lblUserName setHidden:true];
  本文禁止任何網站轉載,嚴厲譴責那些蛀蟲們。
本文首發於,部落格園,請搜尋:部落格園 - 尋自己,查看原版文章
本文首發地址:IOS控制項Label(UILabel) - www.cnblogs.com/xunziji/archive/2012/09/20/2695474.html   7. adjustsFontSizeToFitWidth :  是否根據 Label 寬度來自動調整 text 的大小,預設值為 Yes,bool 類型   8. baselineAdjustment: text的基準位置,不是太長用,沒搞懂~    9. minimumFontSize :  text的最小大小,可以用這個 Property 來阻止 adjustsFontSizeToFitWidth 過於縮放  10. numberOfLines : 設定 text 的最大行,可以防止過高;設為 0 ,即行數不受限制  11. highlighted : 是否高亮(對於label 來說,就是改變text 的顏色),適合在互動的時間使用這個屬性,如 button 被按下,然後改變 button text 的 顏色等,也是說,只有再Label 被按下的時間,比如,一個 tabelcell 裡面又1 個label,當這個 tabelcell 被按下時,背景色改為藍色,這時間,把label 的 highlighted 變成白色會體驗更好 12. highlightedTextColor : 用何種顏色高亮,參考 highlighted   13. shadowColor & shadowOffset : 設定文本的陰影顏色和陰影大小  14. userInteractionEnabled : 是否可以和使用者進行互動,即是否可以回應時間,預設為 NO   UILabel 控制高度,有兩個緯度,一個是 numberOfLines ,另外一個是通過 frame 來設定款高來實現;但是經過使用,發現numberOfLines 只有  0 和 1 比較有意義,即 自動行數 和 1 行顯示,如若是 0,則要配合 設定 frame 來調整高度,即 numberOfLines 可以極端的說,就是用來控制 UILabel 是否為多行顯示,其實用 IsMulitRows 來代替,感覺更好  本文禁止任何網站轉載,嚴厲譴責那些蛀蟲們。
本文首發於,部落格園,請搜尋:部落格園 - 尋自己,查看原版文章
本文首發地址:IOS控制項Label(UILabel) - www.cnblogs.com/xunziji/archive/2012/09/20/2695474.html 範例程式碼: 
 NSString *txt = @"The UILabel class implements a read-only text view. You can use this class to draw one or multiple lines of static text, such as those you might use to identify other parts of your user interface. \n The base UILabel class provides support for both simple and complex styling of the label text. You can also control over aspects of appearance, such as whether the label uses a shadow or draws with a highlight. \n If needed, you can customize the appearance of your text further by subclassing.The default content mode of the UILabel class is UIViewContentModeRedraw. \n This mode causes the view to redraw its contents every time its bounding rectangle changes. \n You can change this mode by modifying the inherited contentMode property of the class.New label objects are configured to disregard user events by default. \n If you want to handle events in a custom subclass of UILabel, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object.";       learnLabel.text = txt;        UIFont *font = [UIFont systemFontOfSize:16];    learnLabel.font = font;        /*Test 1 設定 UIlabel 高度*/    learnLabel.numberOfLines =0;        CGSize txtSize = [txt sizeWithFont:font                       constrainedToSize:CGSizeMake(400, 200)                           lineBreakMode:UILineBreakModeCharacterWrap];        learnLabel.frame = CGRectMake(learnLabel.frame.origin.x, learnLabel.frame.origin.y, txtSize.width, txtSize.height);            /*Test 3 測試 Highlightcolor & highlight    learnLabel.highlighted = true;    learnLabel.textColor = [UIColor greenColor];    learnLabel.highlightedTextColor = [UIColor blueColor];    */        /*Test 4 測試 shadowColor & shadowOffset*/    learnLabel.font =[UIFont systemFontOfSize:34];    learnLabel.shadowColor = [UIColor greenColor];    learnLabel.shadowOffset = CGSizeMake(0.0,0.000001);    learnLabel.backgroundColor = [UIColor grayColor];        /*Test 2 設定自動換行、大小自適應    learnLabel.lineBreakMode = UILineBreakModeWordWrap;    learnLabel.numberOfLines = 0;    CGSize size = [learnLabel sizeThatFits:CGSizeMake(500, 0)];    CGRect rect = learnLabel.frame;    rect.size  =size;    learnLabel.frame = rect;    */

 

本文禁止任何網站轉載,嚴厲譴責那些蛀蟲們。
本文首發於,部落格園,請搜尋:部落格園 - 尋自己,查看原版文章
本文首發地址:IOS控制項Label(UILabel)
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.