iOS中 UITextView文字檢視 技術分享
阿新 • • 發佈:2019-02-05
UITextView:
文字檢視相比與UITextField直觀的區別就是UITextView可以輸入多行文字並且可以滾動顯示瀏覽全文。
UITextField的用處多,UITextView的用法也不少。常見UITextView使用在APP的軟體簡介、內容詳情顯示
小說閱讀顯示、發表空間內容輸入、說說文字框、評論文字框等。UITextView的使用有它本身的代理方法,也有
繼承於父類的方法。本身的方法有從開始編輯到結束編輯的整個過程的監聽,繼承的方法主要是繼承於
UIScrollView的方法,因為關於滾動的控制都屬於UIScrollView的。根據常用經驗,個人添加了在有導航欄
的情況下可能輸入文字框是下移的修復方法和新增文字時內容顯示自動滾動到UITextView底部的實現方法。
#import "TextViewController.h" @interface TextViewController ()<UITextViewDelegate> @property(nonatomic,retain)UILabel *placeholderLabel; @property(nonatomic,retain)UITextView *textView; @end @implementation TextViewController - (void)dealloc { self.placeholderLabel = nil; self.textView = nil; [super dealloc]; }
- (void)viewDidLoad {
[super viewDidLoad];
//新增背景顏色
self.view.backgroundColor = [UIColor cyanColor];
//導航控制器名稱
self.navigationController.title = @"UITextView的建立與使用";
//呼叫介紹TextView的相關屬性
[self configureTextView];
}
介紹TextView的相關屬性
- (void)configureTextView{ //建立TextView檢視 self.textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 80, self.view.frame.size.width - 80, 100)]; //新增到父檢視 [self.view addSubview:self.textView]; //修復文字框的偏移量(下移) self.automaticallyAdjustsScrollViewInsets = NO; //設定UITextView的屬性 //1.設定文字 // self.textView.text = @"你好,我是小韓哥"; //2.設定文字的對齊方式 self.textView.textAlignment = NSTextAlignmentCenter; //3.設定文字字型相關屬性 self.textView.font = [UIFont systemFontOfSize:18]; //等等和UITextField幾乎是一樣的 //設定背景顏色 self.textView.backgroundColor = [UIColor grayColor]; //設定邊框顏色和寬度 self.textView.layer.borderColor = [[UIColor colorWithRed:200.0/255 green:50/255 blue:10/255 alpha:1] CGColor]; self.textView.layer.borderWidth = 2; //7.設定編輯屬性,是否允許編輯(為NO時,只用來顯示,依然可以使用選擇和拷貝功能) // self.textView.editable = NO; self.textView.editable = YES; //模仿UITextField的placeholder屬性 // 在textViewDidBeginEditing和textViewDidEndEditing內寫實現方法。 self.placeholderLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, CGRectGetWidth(self.textView.frame), 20)]; //將UILabel的背景顏色設定為透明顏色clearColor self.placeholderLabel.backgroundColor = [UIColor clearColor]; //設定UILabel的textColor屬性為灰色grayColor self.placeholderLabel.textColor = [UIColor grayColor]; //設定UILabel的text屬性為需要的提示文字 self.placeholderLabel.text = @"請輸入內容"; //設定UILabel的font屬性和self.textView.font一致 self.placeholderLabel.font = self.textView.font; //將UILabel新增到self.textView圖層上 [self.textView addSubview:self.placeholderLabel]; //新增按鈕及監聽 UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom]; addButton.frame = CGRectMake(CGRectGetMaxX(self.textView.frame)+10, 80, 50, 30); addButton.backgroundColor = [UIColor lightGrayColor]; [addButton setTitle:@"新增" forState:UIControlStateNormal]; [addButton addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:addButton]; //新增代理協議 self.textView.delegate = self; [self.placeholderLabel release]; [self.textView release]; }
#pragma mark- 按鈕點選事件實現方法
- (void)btnClick:(UIButton*)sender{
NSLog(@"新增內容:歡迎來到韓俊強的CSDN部落格");
self.textView.text = [self.textView.text stringByAppendingString:@"韓俊強的CSDN部落格\n"];
//輸入文字時自動滾動到底部
/*
1.拼接字串賦值給self.textView.text;
2.計算NSRange自動滾動到底部。
NSRange是一個結構體,其中location是一個以0為開始的index,length是表示物件的長度。他們都是NSUInteger型別
*/
NSRange range = NSMakeRange([self.textView.text length]- 1, 1);
[self.textView scrollRangeToVisible:range];
// [self.view endEditing:YES];
}
#pragma mark- 實現協議裡的方法
//1、將要開始編輯
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
NSLog(@"將要開始編輯?");
return YES;
}
//2、將要完成編輯
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
NSLog(@"將要結束編輯?");
return YES;
}
//3、開始編輯
- (void)textViewDidBeginEditing:(UITextView *)textView{
NSLog(@"開始編輯。");
self.placeholderLabel.text = @"";
}
//4、完成編輯
- (void)textViewDidEndEditing:(UITextView *)textView{
NSLog(@"結束編輯。");
//模仿UTextField的placeholder屬性
if (self.textView.text.length == 0) {
self.placeholderLabel.text = @"請輸入內容";
}else{
self.placeholderLabel.text = @"";
}
}
//5、將要改變內容
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
NSLog(@"將要改變內容?");
return YES;
}
//6、內容完成改變,只有在內容改變時才觸發,而且這個改變內容是手動輸入有效,用本例中得按鈕增加內容不觸發這個操作
- (void)textViewDidChange:(UITextView *)textView{
NSLog(@"改變內容。");
}
//7、內容被選中,幾乎所有操作都會觸發textViewDidChangeSelection,包括點選文字框、增加內容刪除內容
- (void)textViewDidChangeSelection:(UITextView *)textView{
NSLog(@"選中內容。");
}
#pragma mark- 回收鍵盤
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// [self.view endEditing:YES];
[self.textView resignFirstResponder];
}
最終效果: