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];
}
終究效果:
上一篇 管理的藝術
下一篇 Android的屏幕多樣性支持