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];
// 得到系統支持的所有字體名稱
// 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;
// 當變為編輯狀態時,讓該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格式的,必須加后綴名,因為沒有后綴名的時候,系統默認是PNG格式的)
textField.background = [UIImage imageNamed:@"11.png"];
// 文字的居左,居右,居中
textField.textAlignment = NSTextAlignmentCenter;
// 設置文字的垂直位置
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentTop ;
// 不讓進行編輯(交互)
textField.enabled = NO; // enable:改變控件本身的狀態,例如:textfield有可編輯狀態和不可編輯狀態。當設置為NO時,textfield就會從可編輯狀態改變為不可編輯狀態
textField.userInteractionEnabled = NO; // userInteractionEnabled:不可改變控件的狀態,只是把用戶交互關閉了,就是用戶不可對該控件進行操作。
// 添加輔助視圖
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:是按鈕常態,不做任何操作和設置情況下看到的按鈕狀態
// Highlighted: 用戶按住按鈕不松手時看到的狀態
[button setTitle:@"BMW" forState:UIControlStateNormal];
[button setTitle:@"MJ" forState:UIControlStateHighlighted];
// 關閉交互的狀態標題(前一次操作后執行需要的時間較長,暫時關閉交互狀態)
// [button setTitle:@"JMN" forState:UIControlStateDisabled];
// button.enabled = NO;
// 按鈕被選中狀態
// [button setTitle:@"一直被選中" forState:UIControlStateSelected];
// 設置按鈕被選中
// [button setSelected:YES];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
// 當點擊時有光圈效果
button.showsTouchWhenHighlighted = YES;
// 給按鈕添加點擊事件(目標動作機制)
// target:回調方法的執行者
// action:按鈕的回調方法(點擊所觸發的事件),如果回調方法帶參數,這個參數只能是按鈕本身
// controlEvents:觸發事件的點擊模式
[button addTarget:self action:@selector(buttonAction:) forControlEvents:
UIControlEventTouchUpInside];
/*
UIControlEventTouchDown 點擊后觸發事件
UIControlEventTouchDownRepeat 雙擊后觸發事件
UIControlEventTouchDragInside 區域內拖拽觸發事件
UIControlEventTouchDragOutside 區域內拖拽至區域外,拖動過程中不要松手觸發事件
UIControlEventTouchDragEnter 從區域內拖拽出,不松手,在拖拽進去觸發事件
UIControlEventTouchDragExit 拖拽出去、進來、再出去時觸發事件
UIControlEventTouchUpInside 點擊后觸發事件(最常用)
UIControlEventTouchUpOutside
UIControlEventTouchCancel 多點觸控時觸發事件
*/
// 關閉多點觸控
button.multipleTouchEnabled = NO;
textField.tag = 1000; // 給textField添加tag值,tag的作用是,為當前view或者其子類添加標記,當前view或者其子類的父視圖可以通過tag找到該視圖。
[self.window addSubview:button];
return YES;
}
// button的點擊事件實現部分
-(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);
}