iOS中UI控制項的使用

2021-09-30 11:58:00 字數 4795 閱讀 4558

//uilabel的使用

uilabel *label = [[uilabel alloc]initwithframe:cgrectmake(60, 40, 100, 200)];

//設定內容及字型

label.text = @"歡迎歡迎熱淚歡迎!歡迎歡迎熱淚歡迎!歡迎歡迎熱淚歡迎!歡迎歡迎熱淚歡迎!";              //設定文字

=[uicoloryellowcolor];       //設定字型顏色

//產生乙個顏色,使用rgb顏色

uicolor *color = [uicolorcolorwithred:30/255.0 green:80/255.0 blue:123/255.0 alpha:0.7];

label.textcolor = color;

label.font = [uifontsystemfontofsize:22.0];  //設定字型大小

//設定樣式

label.backgroundcolor = [uicolorredcolor];   //背景顏色

label.textalignment =nstextalignmentcenter;  //居中

[label sizetofit];                            //自動的根據內容的寬度和高度調整

= 3;                      //指定顯示的行數

[self.window addsubview:label];

//定義乙個按鈕

uibutton *button1 = [uibuttonbuttonwithtype:uibuttontyperoundedrect];

button1.tag = 2;

//設定button的尺寸

button1.frame = cgrectmake(60, 120, 130,40);

//字型文字

[button1 settitle:@"註冊" forstate:uicontrolstatenormal];//正常狀態下按鈕的文字s

[button1 settitlecolor:[uicoloryellowcolor] forstate:uicontrolstatenormal]; //正常狀態下文字的顏色

[button1 settitle:@"資訊提交中..." forstate:uicontrolstatehighlighted];

[button1 settitlecolor:[uicolor whitecolor]forstate:uicontrolstatehighlighted];

//設定按鈕上文字的大小

button1.titlelabel.font = [uifontsystemfontofsize:20.0];

//背景或者背景顏色

= [uicolorpurplecolor];

//設定正常和高亮狀態下按鈕的背景

[button1 setbackgroundimage:[uiimageimagenamed:@"red"] forstate:uicontrolstatenormal];

[button1 setbackgroundimage:[uiimageimagenamed:@"green"] forstate:uicontrolstatehighlighted];

//新增事件

[button1 addtarget:selfaction:@selector(click:) forcontrolevents:uicontroleventtouchupinside];

//定義乙個uitextfield 文字框

uitextfield *tfname = [[uitextfield alloc]initwithframe:cgrectmake(50, 40, 200, 30)];

//設定字型、顏色

tfname.placeholder =@"請輸入姓名";       //提示文字

tfname.textcolor = [uicolorbluecolor];  //文字顏色

tfname.textalignment = nstextalignmentleft;//對齊方式

//設定背景

uicolor *bgcolor = [uicolorcolorwithred:1.0 green:1.0 blue:224/255.0 alpha:1.0];

tfname.backgroundcolor = bgcolor;    //設定背景顏色

//設定**

tfname.delegate = self;

//設定型別(線型、是否使密碼框)

tfname.borderstyle =uitextborderstyleroundedrect;  //文字框型別

//設定首字母不再大寫

=uitextautocapitalizationtypenone;

//設定是否有提示

tfname.autocorrectiontype =uitextautocorrectiontypeno;

//設定是否顯示清除按鈕(文字框最後)

tfname.clearbuttonmode =uitextfieldviewmodewhileediting;

//設定文字框開始輸入內容的時候,鍵盤的型別

//uikeyboardtypenumbersandpunctuation 數字和特殊符號

tfname.keyboardtype =uikeyboardtypenumbersandpunctuation;

//uikeyboardtypedecimalpad數字的九宮格(數學計算)

tfname.keyboardtype =uikeyboardtypedecimalpad;

//介面中包含了@符號

tfname.keyboardtype =uikeyboardtypeemailaddress;

//數字鍵盤

tfname.keyboardtype =uikeyboardtypenumberpad;

//含有**的特殊字元

tfname.keyboardtype = uikeyboardtypeurl;

//設定一下return 鍵的樣式

tfname.returnkeytype = uireturnkeygo

uislider *slider =[[uislider alloc] initwithframe:cgrectmake(40, 40, 247, 55)];

slider.minimumvalue = 0.0;    //設定最小值

slider.maximumvalue = 100.0;  //設定最大值

slider.value = 38.0;          //表示起始的位置

//設定當前控制項為透明

slider.backgroundcolor = [uicolorclearcolor];

//給滑塊新增乙個事件,用來捕捉滑動的值

//uicontroleventvaluechanged 當控制項的值發生改變的時候,捕捉事件

[slider addtarget:selfaction:@selector(getslidervalue:) forcontrolevents:uicontroleventvaluechanged];

//把滑塊替換成我們自己定義的

[slider setthumbimage:[uiimageimagenamed:@"slider2-1"] forstate:uicontrolstatenormal];

//為了防止拖動的過程中,程式設計原生得控制項,最好設定一下高亮狀態下的

[slider setthumbimage:[uiimageimagenamed:@"slider2-1"] forstate:uicontrolstatehighlighted];

//設定滑塊左側和右側的

//[slider setminimumtrackimage:[uiimageimagenamed:@"slider-1"] forstate:uicontrolstatenormal];  //設定滑塊左側

//設定滑塊右側

//[slider setmaximumtrackimage:[uiimageimagenamed:@"slider-2"] forstate:uicontrolstatenormal];

//設定滑塊左側和右側的

[slider setminimumtrackimage:[uiimageimagenamed:@"slider2"] forstate:uicontrolstatenormal];  //設定滑塊左側

//設定滑塊右側

[slider setmaximumtrackimage:[uiimageimagenamed:@"slider2"] forstate:uicontrolstatenormal];

//定義乙個開關的控制項

uiswitch *switch1 = [[uiswitch alloc]initwithframe:cgrectmake(30, 50, 0, 0)];

//設定預設使開啟

[switch1 seton:yes animated:yes];

//新增目標事件

[switch1 addtarget:selfaction:@selector(switchvalue:) forcontrolevents:uicontroleventvaluechanged];

//開關控制項事件觸發的方法

-(void)switchvalue:(uiswitch*)_selse{

im**iew.image = [uiimageimagenamed:[nsstringstringwithformat:@"%d.png",_page.currentpage+1]];

iOS基本UI控制項總結

自原文 包括以下幾類 繼承自 nsobject 暫列為控制項 uicolor color 顏色 uiimage image 影象 繼承自 uiview 只能相應手勢 uigesturerecognizer 事件 如果本事不響應會將事件 action 向 superview 傳遞 uilabel la...

iOS 中UI控制項的各種對齊方式總結

1.textaligment 文字的水平方向的對齊方式 nstextalignmentleft 0,左對齊 nstextalignmentcenter 1,居中對齊 nstextalignmentright 2,右對齊 uilabel 預設是居中對齊的,並沒有提供頂部對齊的方式 可以使用如下的方式實...

iOS之UI控制項之TableView詳細解釋

建立 uitableview datatable uitableview alloc initwithframe cgrectmake 0,0,320,420 datatable setdelegate self datatable setdatasource self self.view adds...