IOS基础控件--UIImageView、UITextField

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 //    //文本域添加背景Image
20 //    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 *)textField
36 //{
37 //    
38 //}
39 
40 - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
41 {
42     return YES;
43 }
44 - (void)textFieldDidEndEditing:(UITextField *)textField
45 {
46 
47 }
48 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
49 {
50     NSLog(@"%@", string);//每输入一个字符就打印一个,取出输入字符
51     
52     return YES;
53 }
54 - (BOOL)textFieldShouldClear:(UITextField *)textField
55 {
56     
57     
58     return YES;
59 }
60 - (BOOL)textFieldShouldReturn:(UITextField *)textField
61 {
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 }
原文地址:https://www.cnblogs.com/songlei0601/p/5766988.html