~~~
1.創建并初始化
@property (nonatomic, strong) UITextView *textView;
// 創建
self.textView = [[UITextView alloc] initWithFrame:self.view.frame];
// 設置textview里面的字體顏色
self.textView.textColor = [UIColor blackColor];
// 設置字體名字和字體大小
self.textView.font = [UIFont fontWithName:@"Arial" size:18.0];
// 設置代理
self.textView.delegate = self;
// 設置它的背景顏色
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.text = @“hehe”;
// 返回鍵的類型
self.textView.returnKeyType = UIReturnKeyDefault;
// 鍵盤類型
self.textView.keyboardType = UIKeyboardTypeDefault;
// 是否可以拖動
self.textView.scrollEnabled = YES;
2. UITextView退出鍵盤的幾種方式
(1)如果你程序是有導航條的,可以在導航條上面加多一個Done的按鈕,用來退出鍵盤,當然要先實現UITextViewDelegate。
- (void)textViewDidBeginEditing:(UITextView *)textView {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(getOverEditing)];
}
- (void)textViewDidEndEditing:(UITextView *)textView {
self.navigationItem.rightBarButtonItem = nil;
}
- (void)getOverEditing{
[self.textView resignFirstResponder];
}
(2)如果你的textview里不用回車鍵,可以把回車鍵當做退出鍵盤的響應鍵。
#pragma mark - UITextView Delegate Methods
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
(3)還有你也可以自定義其他視圖控件加載到鍵盤上用來退出,比如在彈出的鍵盤上面加一個view來放置退出鍵盤的Done按鈕。
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
UIBarButtonItem * cancelButton= [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
NSArray * buttonsArray = @[cancelButton];
[topView setItems:buttonsArray];
[self.textView setInputAccessoryView:topView];
-(void)dismissKeyBoard
{
[tvTextView resignFirstResponder];
}
3.UITextView自定選擇文字后的菜單
在ViewDidLoad中加入:
- (void)viewDidLoad
{
[super viewDidLoad];
self._textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 100, 300, 200)];
[self.view addSubview:_textView];
UIMenuItem *menuItem = [[UIMenuItem alloc]initWithTitle:@“我是自定義的菜單" action:@selector(didClickCustomMenuAction)];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObject:menuItem]];
[menuItem release];
}
當然上面那個@selector里面的changeColor方法還是自己寫吧,也就是說點擊了我們自定義的菜單項后會觸發的方法。
然后還得在代碼里加上一個方法:
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if(action ==@selector(changeColor) || action == @selector(copy:))
{
if(_textView.selectedRange.length>0)
return YES;
}
return NO;
}
-(void)didClickCustomMenuAction
{
NSLog(@"%@“,__function__);
}
~~~
- 前言
- UITableView詳解(UITableViewCell(一)重中之重)
- UITableView詳解(UITableViewCell(二) 自定義cell)
- UITableView詳解(UITableViewCell(三) cell根據文本長度來自動調整cell高度)
- UITableView詳解(UITableViewCell(四) 增加 刪除 移動)
- UITabBarController詳解(一)UITabBarController的介紹和設置(偷了點懶,直接用了ARC)
- UITabBarController詳解(二)UITabBarController的代理方法以及模態顯示
- UISearchBar詳解(一)基本屬性
- UISearchBar詳解(二)數據刷選類:NSPredicate
- UISearchDisplayController 的使用
- UINavigationController詳解(一)
- UINavigationController詳解(二)UINavigationBar(UIBarButtonItem)
- UINavigationController詳解(三)UIToolBar
- UINavigationController詳解(四)iOS7新特性
- UIScrollView控件詳解
- UISwitch用法-以及-自定義UISwitch控件
- UIAlertView用法
- UILabel 的常見屬性和方法:
- UIPickerView(滾動選擇控制器)
- UIActivityIndicatorView(活動指示器 ---------> 網絡卡后加載,畫面,圖像加載閃爍的圓圈)
- UIStepper
- UIImagePickerController--------圖片選取器
- UITextView
- UITabBarController詳解(三)自定義UITabBarController
- UIWebView基本介紹