UITextField、UILabel、UIButton的基本使用

import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [self.window setRootViewController:[[UIViewController alloc]init]];

// textField(輸入框)的使用
// 初始化并設置frame
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 200, 50)];
// 設置占位字符(提示性文字)
textField.placeholder = @"請輸入手機號/郵箱";
// 設置邊框樣式
textField.borderStyle = UITextBorderStyleLine;
// [textField setBackgroundColor : [UIColor redColor]];
[self.window addSubview:textField];
// 給字體設置顏色
[textField setTextColor:[UIColor blackColor]];
// 改變字體大小
textField.font = [UIFont systemFontOfSize:15];
// 得到系統(tǒng)支持的所有字體名稱
// NSLog(@"%@",[UIFont familyNames]);
// 設置字體樣式、大小
textField.font = [UIFont fontWithName:@"Hiragino Sans" size:15];
// 設置鍵盤樣式
[textField setKeyboardType:UIKeyboardTypeASCIICapable];
// 設置鍵盤的外觀
[textField setKeyboardAppearance:UIKeyboardAppearanceDark];
// 自動糾錯設置
[textField setAutocorrectionType:UITextAutocorrectionTypeYes];
// 設置首字母大寫 (首字母不能大寫時,有可能和鍵盤樣式有關)
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
// 一鍵清除(右邊的小叉號)
textField.clearButtonMode = UITextFieldViewModeUnlessEditing;
// 當變?yōu)榫庉嫚顟B(tài)時,讓該textfeild剛剛所有的輸入清除
textField.clearsOnBeginEditing = YES;

UITextField *textField1 = [[UITextField alloc]initWithFrame:CGRectMake(100, 260, 200, 50)];
textField1.backgroundColor = [UIColor yellowColor];
[self.window addSubview:textField1];


//  打開密碼格式(密碼樣式是否可自定義)
textField.secureTextEntry = YES;
//  設置背景圖像(如果不是PNG格式的,必須加后綴名,因為沒有后綴名的時候,系統(tǒng)默認是PNG格式的)
textField.background = [UIImage imageNamed:@"11.png"];
//  文字的居左,居右,居中
textField.textAlignment = NSTextAlignmentCenter;
//  設置文字的垂直位置
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentTop ;
//  不讓進行編輯(交互)
textField.enabled = NO; // enable:改變控件本身的狀態(tài),例如:textfield有可編輯狀態(tài)和不可編輯狀態(tài)。當設置為NO時,textfield就會從可編輯狀態(tài)改變?yōu)椴豢删庉嫚顟B(tài)
textField.userInteractionEnabled = NO; //  userInteractionEnabled:不可改變控件的狀態(tài),只是把用戶交互關閉了,就是用戶不可對該控件進行操作。

// 添加輔助視圖
UIView *accessoryView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.window.frame), 50)];
accessoryView.backgroundColor = [UIColor blackColor];
textField.inputAccessoryView = accessoryView;

//  自定義輸入視圖(自定義鍵盤)
UIView *inputView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.window.frame), 256)];
textField.inputView =inputView1;

//  左視圖
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 100)];
lable.backgroundColor = [UIColor redColor];
textField.leftView = lable;
lable.text = @"用戶名";
textField.leftViewMode = UITextFieldViewModeAlways;

//  右視圖
UILabel *lable1 = [[UILabel alloc]initWithFrame:CGRectMake(50, 0, 60, 100)];
lable1.backgroundColor = [UIColor greenColor];
lable1.text = @"@sina.com";
textField.rightView = lable1;
textField.rightViewMode = UITextFieldViewModeAlways;

// UIButton的使用
// 按鈕的初始化
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
button.frame = CGRectMake(150, 100, 100, 50);
button.backgroundColor = [UIColor blackColor];
// 設置按鈕標題
// normal:是按鈕常態(tài),不做任何操作和設置情況下看到的按鈕狀態(tài)
// Highlighted: 用戶按住按鈕不松手時看到的狀態(tài)
[button setTitle:@"BMW" forState:UIControlStateNormal];
[button setTitle:@"MJ" forState:UIControlStateHighlighted];
// 關閉交互的狀態(tài)標題(前一次操作后執(zhí)行需要的時間較長,暫時關閉交互狀態(tài))
// [button setTitle:@"JMN" forState:UIControlStateDisabled];
// button.enabled = NO;
// 按鈕被選中狀態(tài)
// [button setTitle:@"一直被選中" forState:UIControlStateSelected];
// 設置按鈕被選中
// [button setSelected:YES];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
// 當點擊時有光圈效果
button.showsTouchWhenHighlighted = YES;

//  給按鈕添加點擊事件(目標動作機制)
//  target:回調方法的執(zhí)行者
//  action:按鈕的回調方法(點擊所觸發(fā)的事件),如果回調方法帶參數(shù),這個參數(shù)只能是按鈕本身
//  controlEvents:觸發(fā)事件的點擊模式

[button addTarget:self action:@selector(buttonAction:) forControlEvents:
 UIControlEventTouchUpInside];

/*
UIControlEventTouchDown  點擊后觸發(fā)事件
UIControlEventTouchDownRepeat  雙擊后觸發(fā)事件
UIControlEventTouchDragInside  區(qū)域內拖拽觸發(fā)事件
UIControlEventTouchDragOutside  區(qū)域內拖拽至區(qū)域外,拖動過程中不要松手觸發(fā)事件
UIControlEventTouchDragEnter  從區(qū)域內拖拽出,不松手,在拖拽進去觸發(fā)事件
UIControlEventTouchDragExit  拖拽出去、進來、再出去時觸發(fā)事件
UIControlEventTouchUpInside  點擊后觸發(fā)事件(最常用)
UIControlEventTouchUpOutside
UIControlEventTouchCancel  多點觸控時觸發(fā)事件
*/

//  關閉多點觸控
button.multipleTouchEnabled = NO;

textField.tag = 1000; // 給textField添加tag值,tag的作用是,為當前view或者其子類添加標記,當前view或者其子類的父視圖可以通過tag找到該視圖。

[self.window addSubview:button];
return YES;

}

// button的點擊事件實現(xiàn)部分
-(void)buttonAction:(id)sender{
UIButton button = (UIButton)sender;
[button setTitle:@"hello world" forState:UIControlStateNormal];
self.window.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
NSLog(@"@@@@@");

//  點擊按鈕可以得到文本輸入框中的文字
//  textField不能是全局變量或者屬性

//  父視圖通過tag值得到field

UITextField *tagTextField = (UITextField *)[self.window viewWithTag:1000];
//  得到文本框輸入的內容
NSString *textFieldStr = tagTextField.placeholder;
NSLog(@"%@",textFieldStr);

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容