iOS中的UITextView文字輸入光標(biāo)使用技巧小結(jié)
1.創(chuàng)建并初始化
@property (nonatomic, strong) UITextView *textView; // 創(chuàng)建 self.textView = [[UITextView alloc] initWithFrame:self.view.frame]; // 設(shè)置textview里面的字體顏色 self.textView.textColor = [UIColor blackColor]; // 設(shè)置字體名字和字體大小 self.textView.font = [UIFont fontWithName:@"Arial" size:18.0]; // 設(shè)置代理 self.textView.delegate = self; // 設(shè)置它的背景顏色 self.textView.backgroundColor = [UIColor whiteColor]; self.textView.text = @“hehe”; // 返回鍵的類型 self.textView.returnKeyType = UIReturnKeyDefault; // 鍵盤(pán)類型 self.textView.keyboardType = UIKeyboardTypeDefault; // 是否可以拖動(dòng) self.textView.scrollEnabled = YES;
2. UITextView退出鍵盤(pán)的幾種方式
(1)如果你程序是有導(dǎo)航條的,可以在導(dǎo)航條上面加多一個(gè)Done的按鈕,用來(lái)退出鍵盤(pán),當(dāng)然要先實(shí)現(xiàn)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里不用回車鍵,可以把回車鍵當(dāng)做退出鍵盤(pán)的響應(yīng)鍵。
#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)還有你也可以自定義其他視圖控件加載到鍵盤(pán)上用來(lái)退出,比如在彈出的鍵盤(pán)上面加一個(gè)view來(lái)放置退出鍵盤(pán)的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]; }
當(dāng)然上面那個(gè)@selector里面的changeColor方法還是自己寫(xiě)吧,也就是說(shuō)點(diǎn)擊了我們自定義的菜單項(xiàng)后會(huì)觸發(fā)的方法。
然后還得在代碼里加上一個(gè)方法:
-(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.設(shè)置UITextView內(nèi)邊距
當(dāng)我們因?yàn)橐恍┬枨髮ITextView當(dāng)成UILabel使用(為了使用UITextView自帶的復(fù)制,粘貼,選擇功能),這時(shí)我們只需要禁用UITextView的幾個(gè)屬性就行了
textView.editable = NO;//不可編輯 textView.scrollEnabled = NO;//不可滾動(dòng) textView.editable = NO;//不可編輯 textView.scrollEnabled = NO;//不可滾動(dòng)
這樣就ok;
但是當(dāng)我們?cè)趯?shí)際運(yùn)用時(shí),想計(jì)算文字的大小并設(shè)置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];
但是通過(guò)這種方法在UILabel上使用沒(méi)有任何問(wèn)題,但是在UITextView是卻不行,文字總是顯示不全,不管你主動(dòng)寫(xiě)多了高度給它,當(dāng)文字不一樣了雙會(huì)顯示不全或顯示高度過(guò)多;
可以用下面的方法試一下
[self.articleLabel setContentInset:UIEdgeInsetsMake(-10, -5, -15, -5)];//設(shè)置UITextView的內(nèi)邊距 [self.articleLabel setTextAlignment:NSTextAlignmentLeft];//并設(shè)置左對(duì)齊 [self.articleLabel setContentInset:UIEdgeInsetsMake(-10, -5, -15, -5)];//設(shè)置UITextView的內(nèi)邊距 [self.articleLabel setTextAlignment:NSTextAlignmentLeft];//并設(shè)置左對(duì)齊
- iOS基于 UILabel實(shí)現(xiàn)文字添加描邊功能
- iOS中UILabel設(shè)置居上對(duì)齊、居中對(duì)齊、居下對(duì)齊及文字置頂顯示
- iOS如何將UIButton中的圖片與文字上下對(duì)齊詳解
- 詳解IOS 利用storyboard修改UITextField的placeholder文字顏色
- iOS改變UITextField占位文字顏色的三種方法
- iOS開(kāi)發(fā)中Swift3 監(jiān)聽(tīng)UITextView文字改變的方法(三種方法)
- IOS開(kāi)發(fā)UIButton(左邊圖片右邊文字效果)
- iOS設(shè)置UIButton文字顯示位置和字體大小、顏色的方法
- iOS應(yīng)用中UILabel文字顯示效果的常用設(shè)置總結(jié)
- iOS開(kāi)發(fā)UI之弧形文字
相關(guān)文章
Unity移動(dòng)端的復(fù)制要這么寫(xiě)示例代碼
這篇文章主要給大家介紹了關(guān)于Unity移動(dòng)端的復(fù)制的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08iOS開(kāi)發(fā)UI篇—xib的簡(jiǎn)單使用實(shí)例
本篇文章主要介紹了iOS開(kāi)發(fā)UI篇—xib的簡(jiǎn)單使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11iOS開(kāi)發(fā)中的幾個(gè)手勢(shì)操作實(shí)例分享
這篇文章主要介紹了iOS開(kāi)發(fā)中的幾個(gè)手勢(shì)操作實(shí)例分享,編寫(xiě)代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-09-09iOS中創(chuàng)建Model的最佳實(shí)踐記錄
這篇文章主要給大家介紹了關(guān)于iOS中創(chuàng)建Model的最佳實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用iOS具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10iOS表情鍵盤(pán)的簡(jiǎn)單實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了iOS表情鍵盤(pán)的簡(jiǎn)單實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03