IOS控件Label(UILabel)


前段時間已經把 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. 默認是不支持事件的,但是可以通過自定義使其支持
 
5. 在代碼中控制換行,需要添加字符 '\n'
 
6. 在 XCode 編輯模式下換行,需要按住 'Control + Enter' 按鍵
 
 
本文禁止任何網站轉載,嚴厲譴責那些蛀蟲們。
本文首發於,博客園,請搜索:博客園 - 尋自己,查看原版文章
本文首發地址: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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM