UILabel、UITextview、UITextField

UILabel

  • 初始化
1
UILabel *label = [[UILabel alloc] init];
  • 基本属性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// frame设置
labelA.frame = CGRectMake(90, 5, 150, 20);

// 设置文本内容
@property(nullable, nonatomic,copy) NSString *text;
label.text = @"文本内容";

// 文本字体
@property(null_resettable, nonatomic,strong) UIFont *font;
label.font = [UIFont fontWithName:@"PingFangSC-Light" size:16];

// 文本字体颜色
@property(null_resettable, nonatomic,strong) UIColor *textColor;
label.textColor = [UIColor colorWithRed:66/255.0 green:66/255.0 blue:66/255.0 alpha:1/1.0];
  • 设置文本对其方式
1
@property(nonatomic) NSTextAlignment textAlignment;

NSTextAlignmentNatural 默认方式; ios 9 之前是NSTextAlignmentLeft(左对齐)
// default is NSTextAlignmentNatural (before iOS 9, the default was NSTextAlignmentLeft)

1
2
3
4
5
NSTextAlignmentLeft : 左对齐
NSTextAlignmentCenter : 居中
NSTextAlignmentRight : 右对齐

label.textAlignment = NSTextAlignmentCenter;
  • 行数设置
1
2
3
4
5
6
7
@property(nonatomic) NSInteger numberOfLines;

// 行数不限
self.labelA.numberOfLines = 0;

// 具体行数设置具体数字
self.labelA.numberOfLines = 2;

UITextview

  • UITextview的部分基础属性设置跟UILabel差不多
1
2
3
4
@property(null_resettable,nonatomic,copy) NSString *text;
@property(nullable,nonatomic,strong) UIFont *font;
@property(nullable,nonatomic,strong) UIColor *textColor;
@property(nonatomic) NSTextAlignment textAlignment;
  • UITextViewDelegate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 将要开始编辑
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;

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

// 是否开始编辑
- (void)textViewDidBeginEditing:(UITextView *)textView;

// 是否结束编辑
- (void)textViewDidEndEditing:(UITextView *)textView;

// 内容将要发生改变编辑
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;

// 内容发生改变时调用 可以在此方法中设置文本框高度的自适应
- (void)textViewDidChange:(UITextView *)textView;

// 焦点发生改变- (void)textViewDidChangeSelection:(UITextView *)textView;
  • 退出键盘

因为UITextview是一个文本编辑控件在编辑时键盘是自动跳出显示的,然而在编辑完成时键盘却不会自动跳出需要手动设置方法让键盘消失。

在模拟器运行中有可能出现点击UITextView后出现光标但为跳出模拟器键盘,使用电脑键盘可以输入。要使用模拟器自带键盘可以通过Shift + Command + K设置

  • 1.如果你程序是有导航条的,可以在导航条上面加多一个Done的按钮,用来退出键盘,当然要先实现UITextViewDelegate。
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
// 添加导航栏按钮
- (void)viewDidLoad {
[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];

self.textView = [[UITextView alloc] init];
self.textView.backgroundColor = [UIColor greenColor];
self.textView.frame = CGRectMake(50, 100, 200, 300);
[self.view addSubview:self.textView];
self.textView.delegate = self; // *代理设置别忘了

UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithTitle:@"down"
style:UIBarButtonItemStyleDone
target:self
action:@selector(leaveEditMode)];

self.navigationItem.rightBarButtonItem = done;
}

// 通过按钮让UITextview注销第一响应
- (void)leaveEditMode {
[self.textView resignFirstResponder];

}
  • 如果你的textview里不用回车键,可以把return键当做退出键盘的响应键
1
2
3
4
5
6
7
8
9
10
11
12
13
14
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{

if ([text isEqualToString:@"\n"]) {

[textView resignFirstResponder];

return NO;

}

return YES;

}
  • 点击textview外的空白视图退出键盘(一般是当前的视图)
1
2
3
4
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
  • NSNotification-通知
1
2
3
UIKIT_EXTERN NSNotificationName const UITextViewTextDidBeginEditingNotification; //开始编辑时发送通知
UIKIT_EXTERN NSNotificationName const UITextViewTextDidChangeNotification; // 文本发生变化时发送通知
UIKIT_EXTERN NSNotificationName const UITextViewTextDidEndEditingNotification; // 结束编辑时发送通知

UITextField

UITextField的用法基本与UITextview相同.UITextField也有自己的代理方法UITextFieldDelegate

区别:

  • UITextField: 只能输入一行,不可以滚动,可以设置提醒文字。

  • UITextView: 能输入多行,可以滚动,不可以设置提醒文字。

该三个控件都具有文本显示的作用
UILabel:显示的只读文本无法编辑,可以根据文字内容自动换行
UITextView:可编辑文本内容,也可进行换行。
UITextField:可编辑文本但无法换行,只能显示一行。