iOS應(yīng)用開發(fā)中的文字選中操作控件UITextView用法講解
1.創(chuàng)建并初始化
創(chuàng)建UITextView的文件,并在.h文件中寫入如下代碼:
#import <UIKit/UIKit.h>
@interface TextViewController : UIViewController <UITextViewDelegate>
{
UITextView *textView;
}
@property (nonatomic, retain) UITextView *textView;
@end
在.m文件中初始化這個(gè)textview,寫入代碼如下:
self.textView = [[[UITextView alloc] initWithFrame:self.view.frame]autorelease]; //初始化大小并自動(dòng)釋放
self.textView.textColor = [UIColor blackColor];//設(shè)置textview里面的字體顏色
self.textView.font = [UIFont fontWithName:@"Arial" size:18.0];//設(shè)置字體名字和字體大小
self.textView.delegate = self;//設(shè)置它的委托方法
self.textView.backgroundColor = [UIColor whiteColor];//設(shè)置它的背景顏色
self.textView.text = @"Now is the time for all good developers tocome to serve their country.\n\nNow is the time for all good developers to cometo serve their country.";//設(shè)置它顯示的內(nèi)容
self.textView.returnKeyType = UIReturnKeyDefault;//返回鍵的類型
self.textView.keyboardType = UIKeyboardTypeDefault;//鍵盤類型
self.textView.scrollEnabled = YES;//是否可以拖動(dòng)
self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自適應(yīng)高度
[self.view addSubview: self.textView];//加入到整個(gè)頁面中
2. UITextView退出鍵盤的幾種方式
因?yàn)槟泓c(diǎn)擊UITextView會(huì)出現(xiàn)鍵盤,如果你退出鍵盤,有如下幾種方式:
(1)如果你程序是有導(dǎo)航條的,可以在導(dǎo)航條上面加多一個(gè)Done的按鈕,用來退出鍵盤,當(dāng)然要先實(shí)UITextViewDelegate。代碼如下:
- (void)textViewDidBeginEditing:(UITextView *)textView {
UIBarButtonItem *done = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leaveEditMode)] autorelease];
self.navigationItem.rightBarButtonItem = done;
}
- (void)textViewDidEndEditing:(UITextView *)textView {
self.navigationItem.rightBarButtonItem = nil;
}
- (void)leaveEditMode {
[self.textView resignFirstResponder];
}
(2)如果你的textview里不用回車鍵,可以把回車鍵當(dāng)做退出鍵盤的響應(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;
}
這樣無論你是使用電腦鍵盤上的回車鍵還是使用彈出鍵盤里的return鍵都可以達(dá)到退出鍵盤的效果。
(3)還有你也可以自定義其他加載鍵盤上面用來退出,比如在彈出的鍵盤上面加一個(gè)view來放置退出鍵盤的Done按鈕。
代碼如下:
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
[topView setBarStyle:UIBarStyleBlack];
UIBarButtonItem * helloButton = [[UIBarButtonItem alloc]initWithTitle:@"Hello" style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
NSArray * buttonsArray = [NSArray arrayWithObjects:helloButton,btnSpace,doneButton,nil];
[doneButton release];
[btnSpace release];
[helloButton release];
[topView setItems:buttonsArray];
[tvTextView setInputAccessoryView:topView];
-(IBAction)dismissKeyBoard
{
[tvTextView resignFirstResponder];
}
(4)設(shè)置UITextView圓角問題
做法是在 #import QuartzCore/QuartzCore.h 后,便能調(diào)用[textView.layer setCornerRadius:10]; 來把 UITextView 設(shè)定圓角
(5)UITextView根據(jù)文本大小自適應(yīng)高度
通過實(shí)現(xiàn)文本字?jǐn)?shù)來確定高度,如下:
NSString * desc = @"Description it is a test font, and don't become angry for which i use to do here.Now here is a very nice party from american or not!";
CGSize size = [desc sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(240, 2000) lineBreakMode:UILineBreakModeWordWrap];
只有UILabel需要定義的numberoflines為0,即不做行數(shù)的限制。如下:
[label setNumberOfLines:0];
[label setFrame:CGRectMake(40, 135, 240, size.height+10)];
[label setText:desc];
(6)UITextView自定選擇文字后的菜單
在ViewDidLoad中加入:
UIMenuItem *menuItem = [[UIMenuItem alloc]initWithTitle:@"分享到新浪微博" action:@selector(changeColor:)];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObject:menuItem]];
[menuItem release];
當(dāng)然上面那個(gè)@selector里面的changeColor方法還是自己寫吧,也就是說點(diǎn)擊了我們自定義的菜單項(xiàng)后會(huì)觸發(fā)的方法。
然后還得在代碼里加上一個(gè)方法:
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if(action ==@selector(changeColor:))
{
if(textView.selectedRange.length>0)
return YES;
}
return NO;
}
實(shí)現(xiàn)后如下圖:
3.一些個(gè)性化定制
總體來說個(gè)性化定制UITextView中的內(nèi)容有兩種方法:
(1)從文件中讀取內(nèi)容到UITextView,這個(gè)個(gè)人感覺使用rtfd和rtf格式文件效果非常好。
(2)使用NSAttributeString進(jìn)行定制
具體方法如下:
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineHeightMultiple = 20.f;
paragraphStyle.maximumLineHeight = 25.f;
paragraphStyle.minimumLineHeight = 15.f;
paragraphStyle.firstLineHeadIndent = 20.f;
paragraphStyle.alignment = NSTextAlignmentJustified;
NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:14], NSParagraphStyleAttributeName:paragraphStyle, NSForegroundColorAttributeName:[UIColor colorWithRed:76./255. green:75./255. blue:71./255. alpha:1]
};
textView.attributedText = [[NSAttributedString alloc]initWithString:content attributes:attributes];
當(dāng)然也可以初始化一個(gè)NSMutableAttributedString,然后向里面添加文字樣式,最后將它賦給textView的AttributedText即可
NSMutableAttributedString *atr = [[NSMutableAttributedString alloc]initWithString:detail];
[atr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, detail.length)];
textView.attributedText = atr;
另外,對(duì)于textview中的鏈接樣式,同樣也可以定制
NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor],
NSUnderlineColorAttributeName: [UIColor blackColor],
NSUnderlineStyleAttributeName: @(NSUnderlinePatternDash)};
self.linkTextAttributes = linkAttributes;
這里只是個(gè)簡(jiǎn)單的例子,具體還有很多屬性可以自行參考頭文件
- iOS開發(fā)中UIDatePicker控件的使用方法簡(jiǎn)介
- 學(xué)習(xí)iOS開關(guān)按鈕UISwitch控件
- 詳解iOS開發(fā)中UIPickerView控件的使用方法
- iOS App開發(fā)中的UIPageControl分頁控件使用小結(jié)
- iOS開發(fā)中UIImageView控件的常用操作整理
- 詳解iOS App開發(fā)中改變UIButton內(nèi)部控件的基本方法
- 詳解iOS開發(fā)中UItableview控件的數(shù)據(jù)刷新功能的實(shí)現(xiàn)
- iOS應(yīng)用開發(fā)中視圖控件UIWindow的基本使用教程
- iOS App中UIPickerView選擇欄控件的使用實(shí)例解析
- iOS中各種UI控件屬性設(shè)置示例代碼
相關(guān)文章
iOS 把圖片保存到相冊(cè),并獲取圖片文件名的實(shí)例
下面小編就為大家分享一篇iOS 把圖片保存到相冊(cè),并獲取圖片文件名的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12iOS設(shè)計(jì)模式——Category簡(jiǎn)單介紹
這篇文章主要介紹了iOS設(shè)計(jì)模式——Category簡(jiǎn)單介紹,有興趣學(xué)習(xí)的同學(xué)可以了解一下。2016-11-11iOS粒子路徑移動(dòng)效果 iOS實(shí)現(xiàn)QQ拖動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了iOS粒子路徑移動(dòng)效果,iOS實(shí)現(xiàn)QQ拖動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07僅需幾行代碼實(shí)現(xiàn)方便易用的狀態(tài)欄指示器
本文通過僅僅數(shù)行代碼實(shí)現(xiàn)了非常方便易用的狀態(tài)欄指示器,比如微博項(xiàng)目的微博數(shù)提醒框,需要的朋友可以參考下2015-08-08iOS中利用CAGradientLayer繪制漸變色的方法實(shí)例
有時(shí)候iOS開發(fā)中需要使用到漸變色,來給圖片或者view蓋上一層,使其顯示效果更好,所以這篇文章主要給大家介紹了關(guān)于iOS中利用CAGradientLayer繪制漸變色的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-11-11在uiview 的tableView中點(diǎn)擊cell進(jìn)入跳轉(zhuǎn)到另一個(gè)界面的實(shí)現(xiàn)方法
這篇文章主要介紹了在uiview 的tableView中點(diǎn)擊cell進(jìn)入跳轉(zhuǎn)到另一個(gè)界面的實(shí)現(xiàn)方法,首先重寫uiviewcontrol方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10iOS中UIAlertView3秒后消失的兩種實(shí)現(xiàn)方法
這篇文章主要介紹了iOS中UIAlertView3秒后消失的兩種實(shí)現(xiàn)方法,實(shí)現(xiàn)方法涉及到NSTimer和PerformSelector:withObject:afterDelay:方法的結(jié)合使用,需要的朋友可以參考下2017-12-12