ios - UILabel

創(chuàng)建:
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];

  • 常用屬性

    1.顯示文字

    label.text = @"我是文本";

    2.文字顏色(默認(rèn)黑色)

    label.textColor = [UIColor redColor];

    3.字體及其大小

    label.font = [UIFont systemFontOfSize:15];

    4.文字對(duì)其方式

    label.textAlignment = NSTextAlignmentLeft;

    typedef NS_ENUM(NSInteger, NSTextAlignment) {
     NSTextAlignmentLeft      = 0,    // 沿左邊對(duì)齊
     NSTextAlignmentCenter    = 1,    // 中心對(duì)齊
     NSTextAlignmentRight     = 2,    // 沿右邊對(duì)齊
     NSTextAlignmentJustified = 3,    // 最后一行自然對(duì)齊
     NSTextAlignmentNatural   = 4,    // 默認(rèn)
    

} NS_ENUM_AVAILABLE_IOS(6_0);

    
5.文字顯示的行數(shù)(0代表無(wú)限)

label.numberOfLines = 0; 
    
6.富文本文字(帶屬性)

label.attributedText = [[NSAttributedString alloc] initWithString:@"我是attri" attributes:@{NSBackgroundColorAttributeName:[UIColor redColor]}];
    
7.文字?jǐn)?shù)超標(biāo)處理

label.lineBreakMode=NSLineBreakByCharWrapping;


typedef NS_ENUM(NSInteger, NSLineBreakMode) {
NSLineBreakByWordWrapping = 0, // 邊緣處理 (默認(rèn))
NSLineBreakByCharWrapping, // 處理不合適的字符
NSLineBreakByClipping, // 簡(jiǎn)單線(xiàn)性處理
NSLineBreakByTruncatingHead, // 溢出的頭部用省略號(hào)處理
NSLineBreakByTruncatingTail, // 溢出的尾部用省略號(hào)處理
NSLineBreakByTruncatingMiddle // 溢出的中間用省略號(hào)處理
} NS_ENUM_AVAILABLE(10_0, 6_0);

*  ####中度使用屬性      
8.用戶(hù)交互 (默認(rèn)NO)

label.userInteractionEnabled = NO;

9.是否有效(默認(rèn)YES,灰色)

label.enabled=NO;
       
10.陰影偏移量

label.shadowOffset = CGSizeMake(10, 10);
    
11.陰影顏色

label.shadowColor = [UIColor blackColor];
        
12.文字垂直方向?qū)R(只有文本行數(shù)為1時(shí)有效)

 label.baselineAdjustment= UIBaselineAdjustmentAlignBaselines;

typedef NS_ENUM(NSInteger, UIBaselineAdjustment) {
UIBaselineAdjustmentAlignBaselines = 0, // 只有文本行數(shù)為1是有效 (默認(rèn)) UIBaselineAdjustmentAlignCenters, // 文本中線(xiàn)于label中線(xiàn)對(duì)齊
UIBaselineAdjustmentNone, // 文本最低端與label中線(xiàn)對(duì)齊
};
備注:
這個(gè)屬性只有當(dāng)adjustsFontSizeToFitWidth=yes的時(shí)候而且numberOfLines是1的時(shí)候才起作用
若是UIBaselineAdjustmentAlignBaselines 的話(huà) 變小之后的字體和以前的字體 和label的y值的距離是不變的
若是UIBaselineAdjustmentAlignCenters的話(huà) 變小之后的字體和以前字體的中間center.y是不變的
若是UIBaselineAdjustmentNone的話(huà) 變小之后的字體和以前的字體距離label底部的距離是不變的

       
13.寬度自適應(yīng)大小 (默認(rèn)NO) 

label.adjustsFontSizeToFitWidth = YES;
       
14.最小適應(yīng)大小

label.minimumScaleFactor = 15;
       
15.陰影顏色

label.shadowColor=[UIColor blackColor];
          
16.高亮狀態(tài)

label.highlighted = YES;
          
17.高亮狀態(tài)文字顏色

label.highlightedTextColor = [UIColor redColor]; 
          
18.自動(dòng)布局添加約束中,若文本超過(guò)了指定的最大寬度的時(shí)候 文本會(huì)另起一行 從而增加了label的高度

label.preferredMaxLayoutWidth = 20;

* ####方法 
1.返回文本繪制矩形

  -(CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;

2.文本繪制函數(shù)

-(void)drawTextInRect:(CGRect)rect;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容