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__); }
4.設置UITextView內邊距
當我們由於一些需求將UITextView當做UILabel應用(為了應用UITextView自帶的復制,粘貼,選擇功效),這時候我們只須要禁用UITextView的幾個屬性就好了
textView.editable = NO;//弗成編纂 textView.scrollEnabled = NO;//弗成轉動 textView.editable = NO;//弗成編纂 textView.scrollEnabled = NO;//弗成轉動
如許就ok;
然則當我們在現實應用時,想盤算文字的年夜小並設置UITextView的顯示年夜小
UIFont *font = [UIFont systemFontOfSize:14.0f]; //指定字符串的年夜小 [textView setText:content]; CGSize textSize = [content sizeWithFont:font constrainedToSize:CGSizeMake(200, 2000) lineBreakMode:UILineBreakModeCharacterWrap]; CGRect articleframe = [articleLabel frame]; textView.size.height = textSize.height ; textView.size.width = textSize.width; [textView setFrame:articleframe]; UIFont *font = [UIFont systemFontOfSize:14.0f]; //指定字符串的年夜小 [textView setText:content]; CGSize textSize = [content sizeWithFont:font constrainedToSize:CGSizeMake(200, 2000) lineBreakMode:UILineBreakModeCharacterWrap]; CGRect articleframe = [articleLabel frame]; textView.size.height = textSize.height ; textView.size.width = textSize.width; [textView setFrame:articleframe];
然則經由過程這類辦法在UILabel上應用沒有任何成績,然則在UITextView是卻不可,文字老是顯示不全,不論你自動寫多了高度給它,當文字紛歧樣了雙會顯示不全或顯示高渡過多;
可以用上面的辦法試一下
[self.articleLabel setContentInset:UIEdgeInsetsMake(-10, -5, -15, -5)];//設置UITextView的內邊距 [self.articleLabel setTextAlignment:NSTextAlignmentLeft];//並設置左對齊 [self.articleLabel setContentInset:UIEdgeInsetsMake(-10, -5, -15, -5)];//設置UITextView的內邊距 [self.articleLabel setTextAlignment:NSTextAlignmentLeft];//並設置左對齊
【iOS中的UITextView文字輸出光標應用技能小結】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!