#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 = @"文本內(nèi)容";
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自動(dòng)檢測內(nèi)部的電話號碼 網(wǎng)站link email
UIView *keyBoardView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
keyBoardView.backgroundColor = [UIColor redColor];
self.textView.inputView = keyBoardView;//在輸入鍵盤上方添加一個(gè)view 可以在這個(gè)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;
//輸入內(nèi)容發(fā)生了改變
- (void)textViewDidChange:(UITextView *)textView;
- (void)textViewDidChangeSelection:(UITextView *)textView;
@end
textView 沒有提示文字功能 模仿textField 給textView加一個(gè)提示信息
思路和原理: 放一個(gè)Label在textview 上一層 剛好覆蓋在textview 輸入的位置 判斷textview中的文字是否為空 如果為空 將 label的hidden 設(shè)置為NO else 設(shè)置為 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 都有
實(shí)現(xiàn)微信輸入框中自動(dòng)換行 超出四行后 可以滾動(dòng)的效果
-(void)textViewDidChange:(UITextView *)textView{
//計(jì)算當(dāng)前textView有多高
CGFloat height = ceilf([self.textView sizeThatFits:CGSizeMake(self.textView.bounds.size.width, MAXFLOAT)].height);
NSLog(@"height = %.2f",height);
// 計(jì)算最大高度 = (每行高度 * 總行數(shù) + 文字上下間距)
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);
}];
});
}
}