多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > iOS中 UITextView文本視圖 技術分享

iOS中 UITextView文本視圖 技術分享

來源:程序員人生   發布時間:2016-03-23 09:16:14 閱讀次數:2542次

 UITextView:

 文本視圖相比與UITextField直觀的區分就是UITextView可以輸入多行文字并且可以轉動顯示閱讀全文。

 UITextField的用途多,UITextView的用法也很多。常見UITextView使用在APP的軟件簡介、內容詳情顯示

 小說瀏覽顯示、發表空間內容輸入、說說文本框、評論文本框等。UITextView的使用有它本身的代理方法,也有

 繼承于父類的方法。本身的方法有從開始編輯到結束編輯的全部進程的監聽,繼承的方法主要是繼承于

 UIScrollView的方法,由于關于轉動的控制都屬于UIScrollView的。根據經常使用經驗,個人添加了在有導航欄

 的情況下可能輸入文本框是下移的修復方法和添加文字時內容顯示自動轉動到UITextView底部的實現方法。

#import "TextViewController.h" @interface TextViewController ()@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幾近是1樣的 //設置背景色彩 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.font1致 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博客 "]; //輸入文字時自動轉動到底部 /* 1.拼接字符串賦值給self.textView.text; 2.計算NSRange自動轉動到底部。 NSRange是1個結構體,其中location是1個以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]; }

終究效果:

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 亚洲综合色自拍一区 | 亚州五月天| 欧洲精品一区二区三区 | 97综合| 亚洲男女天堂 | 麻豆国产成人精品午夜视频 | 亚洲成a人片在线观看www流畅 | 亚洲自拍偷拍视频 | 国产亚洲欧美日韩在线一区 | 亚洲视频精品在线 | 2022亚洲男人天堂 | 亚洲精品国产suv一区88 | 亚洲视频第一页 | 韩日一区二区三区 | 亚洲一区二区观看 | 精品卡通动漫在线观看视频一区 | www在线观看免费视频 | 亚洲精品日韩在线一区 | 老司机福利在线观看 | 免费视频网站在线看视频 | 久久精品一区二区三区中文字幕 | 波多野结衣视频在线免费观看 | 国产精品一区不卡 | 亚洲都市春色系列小说类型 | 国产欧美成人一区二区三区 | 天堂欧美| 亚洲精品国产一区二区三区四区 | 日韩欧美在线观看视频一区二区 | 欧美人成在线 | 一本大道香蕉高清久久 | 欧美一级aⅴ毛片 | 国产h视频在线 | 99久久精品毛片免费播放 | 最近完整中文字幕1 | 在线观看日本永久免费视频 | 久久久91精品国产一区二区三区 | 亚洲一区二区三区在线 | 久久欧美精品欧美九久欧美 | 国产h在线播放 | 玖玖精品视频 | 日韩欧美亚洲视频 |