label 詳細用法

一label基本設置

self.view.backgroundColor = [UIColor redColor];

    //創建第一個標籤控件

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 200, 30)];

    //對位置設置

    //對控件的中心點進行設置

    label.center = self.view.center;

    label.frame = CGRectMake(20, 20, 30, 30);

    

加粗;

[UILabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];

加粗並且傾斜

[UILabel setFont:[UIFont fontWithName:@"Helvetica-BoldOblique" size:20]];

    //顯示文字

    label.text = @"我是美女";

    

    //設置字體大小

    label.font = [UIFont systemFontOfSize:30];

    

    //自適應大小的方法   標籤的大小由字體的大小長度決定

    [label sizeToFit];

    

    //字體的顏色  alpha 透明度 0 - 1   0- 1

    label.textColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:195/255.0 alpha:1];

    

    //Red Green Blue 0 - 255   255  255  255

    //  0 - 1

    //字體對齊格式  右側是枚舉類型

    label.textAlignment = NSTextAlignmentCenter;

    

    //加背景顏色

    label.backgroundColor = [UIColor greenColor];

    

    //顯示出來 將標籤 放到視圖上 進行顯示

    

    [self.view addSubview:label];

    //addSubview 添加子視圖

    

    //不是程序崩潰前提下 問題:

    //第一點  frame是否設置了

    //第二點  是不是加到了父視圖中

    //第三點  背景色和 控件顏色 一樣

 

二.文字自適應

//創建label

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, 200, 999)];

    label.backgroundColor = [UIColor greenColor];

    label.text = @"To be or not to be, that is a question。To be or not to be, that is a question。To be or not to be, that is a question。To be or not to be, that is a question。To be or not to be, that is a question。";

    label.font = [UIFont systemFontOfSize:18];

    label.textColor = [UIColor redColor];

   //設置 label的換行模式

    label.lineBreakMode = NSLineBreakByWordWrapping; //根據單詞進行換行

    //設置label顯示幾行  可以有無限行

    label.numberOfLines = 0;

 

    [label sizeToFit];

    

    [self.view addSubview:label];

 

 

 

 

 

text  property     

 font  property     

 textColor  property     

 textAlignment  property     

 lineBreakMode  property       

 enabled  property     

 Sizing the Label’s Text   

 adjustsFontSizeToFitWidth  property     

 baselineAdjustment  property     

 minimumFontSize  property   無例   

 numberOfLines  property     

 Managing Highlight Values   

 highlightedTextColor  property     

 highlighted  property     

 Drawing a Shadow   

 shadowColor  property     

 shadowOffset  property     

 Drawing and Positioning Overrides   

 – textRectForBounds:limitedToNumberOfLines: 無例    

 – drawTextInRect:  無例   

 Setting and Getting Attributes   

 userInteractionEnabled  property    

 

UILabel垂直居上對齊[label sizeToFit];

//設置文字過長時的顯示格式 

label.lineBreakMode = UILineBreakModeWordWrap;

 

typedefenum {

    UILineBreakModeWordWrap =0,           // Wrap at word boundaries

    UILineBreakModeCharacterWrap,          // Wrap at character boundaries

    UILineBreakModeClip,           //截去多餘部分 Simply clip when it hits the end of the rect截去多餘部分 

    UILineBreakModeHeadTruncation, //截去頭部Truncate at head of line: "...wxyz". Will truncate multiline text on first line

    UILineBreakModeTailTruncation,//截去尾部 Truncate at tail of line: "abcd...". Will truncate multiline text on last line

    UILineBreakModeMiddleTruncation,//截去中間 Truncate middle of line:  "ab...yz". Will truncate multiline text in the middle

} UILineBreakMode;

 

//設置label的行數,這個可以根據上節的UITextView自適應高度 

label.numberOfLines = 2;

label.lineBreakMode = UILineBreakModeWordWrap;

label.textAlignment =  UITextAlignmentCenter;//設置文字對齊位置,居左,居中,居右 

label.text = @ "123" ;//設置顯示文字 

//設置文字顏色,可以有多種顏色可以選擇

label.textColor = [UIColor whiteColor];

label.backgroundColor = [UIColor blackColor];

//設置字體:粗體,正常的是 SystemFontOfSize,調用系統的字體配置 

label.font = [UIFont boldSystemFontOfSize:20];

label.font = [UIFont fontWithName:@ "Arial Rounded MT Bold"  size:(36.0)];

 

//[UIFont fontWithName:@ "Arial" size:14.0]]; //非加粗

 

//設置文本是否高亮和高亮時的顏色

scoreLabel.highlighted = YES; 

scoreLabel.highlightedTextColor = [UIColor orangeColor]; 

//設置陰影的顏色和陰影的偏移位置 

scoreLabel.shadowColor = [UIColor redColor]; 

scoreLabel.shadowOffset = CGSizeMake(1.0,1.0); 

//設置是否能與用戶進行交互 

scoreLabel.userInteractionEnabled = YES;  

//設置label中的文字是否可變,默認值是YES  

scoreLabel.enabled = NO;

 

//設置字體大小是否適應label寬度 

label.adjustsFontSizeToFitWidth = YES; 

//如果adjustsFontSizeToFitWidth屬性設置爲YES,這個屬性就來控制文本基線的行爲

 

coreLabel.baselineAdjustment = UIBaselineAdjustmentNone

 

typedefenum {

    UIBaselineAdjustmentAlignBaselines =0,// default. used when shrinking text to position based on the original baseline

    UIBaselineAdjustmentAlignCenters,

    UIBaselineAdjustmentNone,

} UIBaselineAdjustment;

 

//最小文字號數

minimumFontSize

設置背景色爲透明 

 

 

scoreLabel.backgroudColor=[UIColor clearColor];

自定義的顏色:

scoreLabel.backgroudColor=[UIColor clearColor];

 

UIColor *color = [UIColor colorWithRed:1.0f green:50.0f blue:0.0f alpha:1.0f];

scoreLabel.textColor = [UIColor color]

//UIColor 裏的 RGB 值是CGFloat類型的在0~1範圍內,對應0~255的顏色值範圍。

 

 

 

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

//改變繪文字屬性.重寫時調用super可以按默認圖形屬性繪製,若自己完全重寫繪製函數,就不用調用super了.

- (void)drawTextInRect:(CGRect)rect;

 

eg:

 

UILabel *label = [[UILabelalloc] initWithFrame:CGRectMake(0, 0, 75, 40)];   //聲明UIlbel並指定其位置和長寬
 label.backgroundColor = [UIColorclearColor];   //設置label的背景色,這裏設置爲透明色。
 label.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];   //設置label的字體和字體大小。
//lable的旋轉
 label.transform = CGAffineTransformMakeRotation(0.1);     //設置label的旋轉角度
 label.text = @“helloworld”;   //設置label所顯示的文本
 label.textColor = [UIColorwhiteColor];    //設置文本的顏色
 label.shadowColor = [UIColorcolorWithWhite:0.1falpha:0.8f];    //設置文本的陰影色彩和透明度。
 label.shadowOffset = CGSizeMake(2.0f, 2.0f);     //設置陰影的傾斜角度。
 label.textAlignment = UITextAlignmentCenter;     //設置文本在label中顯示的位置,這裏爲居中。
//換行技巧:如下換行可實現多行顯示,但要求label有足夠的寬度。
 label.lineBreakMode = UILineBreakModeWordWrap;     //指定換行模式
 label.numberOfLines = 2;    // 指定label的行數

 

 

 

 

 

 

讓label自適應裏面的文字,自動調整寬度和高度的

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];這個frame是初設的,沒關係,後面還會重新設置其size。
[label setNumberOfLines:0];
NSString *s = @"string......";
UIFont *font = [UIFont fontWithName:@"Arial" size:12];
CGSize size = CGSizeMake(320,2000);
CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
[label setFrame:CGRectMake:(0,0, labelsize.width, labelsize.height)];
[self.view addSubView:label];
這樣就可以對s賦值讓其自動調整其大小了。


發佈了72 篇原創文章 · 獲贊 10 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章