UITextView

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];

self.textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 667-80, 375, 80)];

self.textView.delegate = self;

self.textView.font = [UIFont systemFontOfSize:20];

self.textView.textColor = [UIColor blackColor];

self.textView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);//添加滚动区域

self.textView.text = @"习大大";

self.textView.scrollEnabled = YES;//是否可滚动

self.textView.editable = YES;//是否可编辑

self.textView.returnKeyType = UIReturnKeyDefault;//返回键的类型

self.textView.keyboardType = UIKeyboardTypeDefault;//键盘类型

self.textView.scrollEnabled = YES;//是否可以拖动

self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自适应高度

self.textView.layer.cornerRadius = 8; //边框设置为圆角

self.textView.layer.borderWidth = 1;

// 设置边框颜色 参数2是一个数组 分别为三基色和alpha分量;
CGFloat color[]={0.5, 0.5, 0.5, 1};
self.textView.layer.borderColor = CGColorCreate(CGColorSpaceCreateDeviceRGB(),color);
self.textView.layer.masksToBounds = YES;

// [self.textView becomeFirstResponder];//获得焦点 程序运行就获取光标

self.myView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.myView.tag = 1000;

[self.view addSubview:self.myView];

[self.myView addSubview:self.textView];

//注册通知,监听键盘弹出事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];

//注册通知,监听键盘消失事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden) name:UIKeyboardDidHideNotification object:nil];
}

//将要开始编辑
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{

return YES;
}

//将要结束编辑
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{

return YES;
}

//开始编辑
- (void)textViewDidBeginEditing:(UITextView *)textView{

}

//结束编辑
- (void)textViewDidEndEditing:(UITextView *)textView{

}

//焦点发生改变
- (void)textViewDidChangeSelection:(UITextView *)textView{

}

//内容发生改变编辑
- (void)textViewDidChange:(UITextView *)textView{

}

//内容将要发生改变编辑

//控制输入文字的长度和内容,可通调用以下代理方法实现
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{

if (range.location>=100){
//控制输入文本的长度
return NO;
}
if ([text isEqualToString:@"\n"]) {
//禁止输入换行
return NO;
}
else{
return YES;
}
}

// 键盘弹出时
-(void)keyboardDidShow:(NSNotification *)notification{

//获取键盘高度
NSValue *keyboardObject = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];

CGRect keyboardRect;

[keyboardObject getValue:&keyboardRect];

//调整放置有textView的view的位置

//设置动画
[UIView beginAnimations:nil context:nil];

//定义动画时间
[UIView setAnimationDuration:kAnimationDuration];

//设置view的frame,往上平移
[(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-keyboardRect.size.height-kViewHeight, 375, kViewHeight)];

[UIView commitAnimations];
}

//键盘消失时
-(void)keyboardDidHidden
{
//定义动画
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kAnimationDuration];
//设置view的frame,往下平移
[(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-kViewHeight, 375, kViewHeight)];
[UIView commitAnimations];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

[self.textView resignFirstResponder];
}
@end
文章目录
|