XCode中UILable的基本用法

2021-06-08 19:27:42 字數 3673 閱讀 3103

//uilable的大小自適應例項

uilabel *mylable = [[uilabel alloc] initwithframe:cgrectmake(0, 0, 2, 2)];//設定位置與大小

[mylable setfont:[uifont fontwithname:@"helvetica" size:20.0]];//格式

[mylable setnumberoflines:0];//行數,只有設為0才能自適應

[mylable setbackgroundcolor:[uicolor clearcolor]];//背景色

mylable.shadowcolor = [uicolor darkgraycolor];//陰影顏色

mylable.shadowoffset = cgsizemake(1., 1.0);//陰影大小

nsstring *text = @"abcdefghijklmnopqrstuvwxyz";

uifont *font = [uifont fontwithname:@"helvetica" size:20.0];

cgsize size = [text sizewithfont:font constrainedtosize:cgsizemake(175.0f, 2000.0f) linebreakmode:uilinebreakmodewordwrap];

cgrect rect = mylable.frame;

rect.size = size;

[mylable setframe:rect];

[mylable settext:text];

mylable.shadowcolor = [uicolor darkgraycolor];//陰影顏色

mylable.shadowoffset = cgsizemake(2.0, 2.0);//陰影大小

[self.view addsubview:mylable];

[mylable release];

//uilable的基本用法

uilabel *lbl1 = [[uilabel alloc] initwithframe:cgrectmake(50.0, 40.0, 200.0, 30.0)];

uilabel *lbl2 = [[uilabel alloc] initwithframe:cgrectmake(50.0, 80.0, 200.0, 50.0)];

uilabel *lbl3 = [[uilabel alloc] initwithframe:cgrectmake(50.0, 140.0, 200.0, 50.0)];

uilabel *lbl4 = [[uilabel alloc] initwithframe:cgrectmake(50.0, 200.0, 200.0, 50.0)];

uilabel *lbl5 = [[uilabel alloc] initwithframe:cgrectmake(50.0, 260.0, 200.0, 50.0)];

uilabel *lbl6 = [[uilabel alloc] initwithframe:cgrectmake(50.0, 320.0, 200.0, 50.0)];

uilabel *lbl7 = [[uilabel alloc] initwithframe:cgrectmake(50.0, 380.0, 200.0, 50.0)];

//設定顯示文字

lbl1.text = @"lable1";

lbl2.text = @"lable2";

lbl3.text = @"lable3--lable3--lable3--lable3--lable3--lable3--lable3--lable3--lable3--lable3--lable3--11個";

lbl4.text = @"lable4--lable4--lable4--lable4--4個";

lbl5.text = @"lable5--lable5--lable5--lable5--lable5--lable5--6個";

lbl6.text = @"lable6";

lbl7.text = @"lable7";

//設定字型:粗體,正常的是systemfontofsize

lbl1.font = [uifont boldsystemfontofsize:20];

//設定文字顏色

lbl1.textcolor = [uicolor orangecolor];

lbl2.textcolor = [uicolor purplecolor];

//設定背景顏色

lbl1.backgroundcolor = [uicolor clearcolor];

lbl2.backgroundcolor = [uicolor colorwithred:0.5f green:30/255.0f blue:0.3f alpha:0.5f];

//設定字型位置

lbl1.textalignment = uitextalignmentright;

lbl2.textalignment = uitextalignmentcenter;

//設定字型的小適應lable的寬度

lbl4.adjustsfontsizetofitwidth = yes;

//設定lable 的行數

lbl5.numberoflines = 2;

//設定高亮

lbl6.highlighted = yes;

lbl6.highlightedtextcolor = [uicolor orangecolor];

//設定陰影

lbl7.shadowcolor = [uicolor redcolor];

lbl7.shadowoffset = cgsizemake(1.0, 1.0);

//設定是否能與使用者進行互動

lbl7.userinteractionenabled = yes;

//設定lable中文字是否可變,預設為yes;

lbl3.enabled = no;

//設定lable中文字過長時的顯示格式

lbl3.linebreakmode = uilinebreakmodemiddletruncation; //截去中間

// typedef enumuilinebreakmode;

//如果adjustsfontsizetofitwidth屬性設定為yes,這個屬性就用來控制文字基線的行為

lbl4.baselineadjustment = uibaselineadjustmentnone;

[self.view addsubview:lbl1];

[self.view addsubview:lbl2];

[self.view addsubview:lbl3];

[self.view addsubview:lbl4];

[self.view addsubview:lbl5];

[self.view addsubview:lbl6];

[self.view addsubview:lbl7];

[lbl1 release];

[lbl2 release];

[lbl3 release];

[lbl4 release];

[lbl5 release];

[lbl6 release];

[lbl7 release];

UILable不同版中的lineBreakMode

label.linebreakmode nslinebreakbyclipping 剪下與文字寬度相同的內容長度,後半部分被刪除。label.linebreakmode nslinebreakbytruncatinghead 前面部分文字以 方式省略,顯示尾部文字內容。label.linebreak...

UILable不同版中的lineBreakMode

label.linebreakmode nslinebreakbyclipping 剪下與文字寬度相同的內容長度,後半部分被刪除。label.linebreakmode nslinebreakbytruncatinghead 前面部分文字以 方式省略,顯示尾部文字內容。label.linebreak...

UILable不同版中的lineBreakMode

label.linebreakmode nslinebreakbyclipping 剪下與文字寬度相同的內容長度,後半部分被刪除。label.linebreakmode nslinebreakbytruncatinghead 前面部分文字以 方式省略,顯示尾部文字內容。label.linebreak...