標籤:
UIImageView:
1 - (void)viewDidLoad {2 [super viewDidLoad];3 4 UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREENWIDTH / 2 - 46, 66, 92, 84)];5 imgView.image = [UIImage imageNamed:@"logo"];6 [self.view addSubview:imgView];7 8 }
UITextField:
- (void)viewDidLoad { [super viewDidLoad]; UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 100, [UIScreen mainScreen].bounds.size.height / 2 - 20, 200, 40)]; textField.placeholder = @"請輸入使用者名稱"; textField.textAlignment = NSTextAlignmentCenter;//置中 //Image路徑 NSString *path = [[NSBundle mainBundle] pathForResource:@"登入" ofType:@"png"]; //文本域添加背景Image textField.background = [UIImage imageWithContentsOfFile:path]; [self.view addSubview:textField]; }
UITextFieldDelegate 協議
1 #import "ViewController.h" 2 3 @interface ViewController () 4 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad {10 [super viewDidLoad];11 12 UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 100, [UIScreen mainScreen].bounds.size.height - 100, 200, 40)];13 14 textField.placeholder = @"請輸入使用者名稱";15 16 textField.textAlignment = NSTextAlignmentCenter;//置中17 // //Image路徑18 // NSString *path = [[NSBundle mainBundle] pathForResource:@"登入" ofType:@"png"];19 // //文本域添加背景Image20 // textField.background = [UIImage imageWithContentsOfFile:path];21 22 [self.view addSubview:textField];23 24 textField.delegate = self;25 26 }27 28 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField//開始編輯,隨著整體往上移動29 {30 [UIView animateWithDuration:0.3 animations:^{self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - 220, self.view.frame.size.width, self.view.frame.size.height);}];31 32 return YES;33 }34 35 //- (void)textFieldDidBeginEditing:(UITextField *)textField36 //{37 // 38 //}39 40 - (BOOL)textFieldShouldEndEditing:(UITextField *)textField41 {42 return YES;43 }44 - (void)textFieldDidEndEditing:(UITextField *)textField45 {46 47 }48 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string49 {50 NSLog(@"%@", string);//每輸入一個字元就列印一個,取出輸入字元51 52 return YES;53 }54 - (BOOL)textFieldShouldClear:(UITextField *)textField55 {56 57 58 return YES;59 }60 - (BOOL)textFieldShouldReturn:(UITextField *)textField61 {62 // NSString *str = textField.text;63 // NSLog(@"%@", str);//擷取輸入字串64 [textField resignFirstResponder];//return鍵盤收縮65 [UIView animateWithDuration:0.1 animations:^{self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + 220, self.view.frame.size.width, self.view.frame.size.height);}];//隨著鍵盤收縮,整體往下移動66 67 return YES;68 }69 70 - (void)didReceiveMemoryWarning {71 [super didReceiveMemoryWarning];72 // Dispose of any resources that can be recreated.73 }74 75 @end
UITextViewDelegate協議
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 30, [UIScreen mainScreen].bounds.size.width - 40, [UIScreen mainScreen].bounds.size.height - 300)]; 5 textView.text = @"雖然autoresizing已經過時,但我們還是有必要瞭解一下的。autoResizing是蘋果早期的螢幕適配的解決辦法,iOS6之前完全可以勝任,因為蘋果手機只有3.5寸的螢幕,在加上手機app很少支援橫屏,所以iOS開發人員基本不用怎麼適配布局,所"; 6 textView.font = [UIFont systemFontOfSize:20]; 7 // textView.userInteractionEnabled = NO;//禁止使用者移動,不常用 8 textView.textColor = [UIColor redColor]; 9 10 [self.view addSubview:textView];11 // textView.delegate = self;//執行<UITextViewDelegate>協議代理方法12 13 }
IOS基礎控制項--UIImageView、UITextField