#import "ViewController.h"
@interface ViewController ()<UITextViewDelegate>
@property(nonatomic,strong)UITextView *textView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.textView = [[UITextView alloc]initWithFrame:CGRectMake(20, 100, self.view.bounds.size.width - 40, 100)];
[self.view addSubview:self.textView];
self.textView.backgroundColor = [UIColor orangeColor];
self.textView.text = @"文本内容";
self.textView.font = [UIFont systemFontOfSize:14];//文字大小
self.textView.textAlignment = NSTextAlignmentLeft;//文本对其方式
self.textView.textColor = [UIColor purpleColor];//文本颜色
self.textView.editable = YES;//是否可以编辑
self.textView.selectable = YES;//是否支持选中
self.textView.delegate = self;//代理
self.textView.dataDetectorTypes = UIDataDetectorTypeAll;//textView自动检测内部的电话号码 网站link email
UIView *keyBoardView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
keyBoardView.backgroundColor = [UIColor redColor];
self.textView.inputView = keyBoardView;//在输入键盘上方添加一个view 可以在这个view 中添加需要的控件
}
- (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;
@end
textView 没有提示文字功能 模仿textField 给textView加一个提示信息
思路和原理: 放一个Label在textview 上一层 刚好覆盖在textview 输入的位置 判断textview中的文字是否为空 如果为空 将 label的hidden 设置为NO else 设置为 YES
_placeHolderLab = [[UILabel alloc]initWithFrame:CGRectMake(self.textView.frame.origin.x+5, self.textView.frame.origin.y+4, 100, 25)];
_placeHolderLab.text = @"请输入xxx,xxx";
_placeHolderLab.font = _textView.font;
_placeHolderLab.textColor = [UIColor lightGrayColor];
[self.view addSubview:_placeHolderLab];
-(void)textViewDidChange:(UITextView *)textView{
if (textView.text.hash == @"".hash) {
self.placeHolderLab.hidden = NO;
}else{
if (self.placeHolderLab.hidden == NO) {
self.placeHolderLab.hidden = YES;
}
}
}
UITextView 继承于 UIScrollView 所以 UIScrollView 有的特性 textview 都有
实现微信输入框中自动换行 超出四行后 可以滚动的效果
-(void)textViewDidChange:(UITextView *)textView{
//计算当前textView有多高
CGFloat height = ceilf([self.textView sizeThatFits:CGSizeMake(self.textView.bounds.size.width, MAXFLOAT)].height);
NSLog(@"height = %.2f",height);
// 计算最大高度 = (每行高度 * 总行数 + 文字上下间距)
CGFloat f = ceil([UIFont systemFontOfSize:14].lineHeight * 5 + self.textView.textContainerInset.top + self.textView.textContainerInset.bottom);
if (height < f) {
self.textView.scrollEnabled = NO;
[UIView animateWithDuration:0.25 animations:^{
CGRect rect = self.textView.frame;
rect.size.height = height;
self.textView.frame = rect;
}];
}else{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.25 animations:^{
self.textView.scrollEnabled = YES;
textView.contentOffset = CGPointMake(0, textView.contentSize.height - textView.bounds.size.height);
}];
});
}
}