一步一步學ios UITextView(多行文字框)控制項的用法詳解(五5.8)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   使用   

本文轉載至 http://wuchaorang.2008.blog.163.com/blog/static/48891852201232014813990/  

1.建立並初始化

建立UITextView的檔案,並在.h檔案中寫入如下代碼:

 

[csharp] view plaincopy 
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface TextViewController : UIViewController <UITextViewDelegate>  
  4. {  
  5.               UITextView *textView;  
  6. }  
  7.   
  8. @property (nonatomic, retain) UITextView *textView;  
  9.   
  10. @end  




 

在.m檔案中初始化這個textview,寫入代碼如下:

 

[csharp] view plaincopy 
  1. self.textView = [[[UITextView alloc] initWithFrame:self.view.frame]autorelease]; //初始化大小並自動釋放  
  2.   
  3. self.textView.textColor = [UIColor blackColor];//設定textview裡面的字型顏色  
  4.   
  5. self.textView.font = [UIFont fontWithName:@"Arial" size:18.0];//設定字型名字和字型大小  
  6.   
  7. self.textView.delegate = self;//設定它的委託方法  
  8.   
  9. self.textView.backgroundColor = [UIColor whiteColor];//設定它的背景顏色  
  10.   
  11.                
  12.   
  13. 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.";//設定它顯示的內容  
  14.   
  15. self.textView.returnKeyType = UIReturnKeyDefault;//返回鍵的類型  
  16.   
  17. self.textView.keyboardType = UIKeyboardTypeDefault;//鍵盤類型  
  18.   
  19. self.textView.scrollEnabled = YES;//是否可以拖動  
  20.   
  21.                
  22.   
  23. self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自適應高度  
  24.   
  25.                
  26.   
  27. [self.view addSubview: self.textView];//加入到整個頁面中  



 

 

 

2. UITextView退出鍵盤的幾種方式

因為你點擊UITextView會出現鍵盤,如果你退出鍵盤,有如下幾種方式:

(1)如果你程式是有導航條的,可以在導航條上面加多一個Done的按鈕,用來退出鍵盤,當然要先實UITextViewDelegate。

代碼如下:

[csharp] view plaincopy 
  1. - (void)textViewDidBeginEditing:(UITextView *)textView {    
  2.   
  3.    UIBarButtonItem *done =    [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leaveEditMode)] autorelease];    
  4.   
  5.    self.navigationItem.rightBarButtonItem = done;        
  6.   
  7. }    
  8.   
  9. - (void)textViewDidEndEditing:(UITextView *)textView {    
  10.   
  11.     self.navigationItem.rightBarButtonItem = nil;    
  12.   
  13. }    
  14.   
  15. - (void)leaveEditMode {    
  16.   
  17.     [self.textView resignFirstResponder];    
  18.   
  19. }    




(2)如果你的textview裡不用斷行符號鍵,可以把斷行符號鍵當做退出鍵盤的響應鍵。
代碼如下:

[csharp] view plaincopy 
  1. #pragma mark - UITextView Delegate Methods    
  2.   
  3. -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text    
  4.   
  5. {    
  6.   
  7.     if ([text isEqualToString:@"\n"]) {    
  8.   
  9.         [textView resignFirstResponder];    
  10.   
  11.         return NO;    
  12.   
  13.     }    
  14.   
  15.     return YES;    
  16.   
  17. }    





這樣無論你是使用電腦鍵盤上的斷行符號鍵還是使用彈出鍵盤裡的return鍵都可以達到退出鍵盤的效果。

(3)還有你也可以自訂其他載入鍵盤上面用來退出,比如在彈出的鍵盤上面加一個view來放置退出鍵盤的Done按鈕。

代碼如下:

 

[csharp] view plaincopy 
  1. UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];    
  2.   
  3.     [topView setBarStyle:UIBarStyleBlack];    
  4.   
  5.     UIBarButtonItem * helloButton = [[UIBarButtonItem alloc]initWithTitle:@"Hello" style:UIBarButtonItemStyleBordered target:self action:nil];          
  6.   
  7.     UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];    
  8.   
  9.         
  10.   
  11.     UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];    
  12.   
  13.     NSArray * buttonsArray = [NSArray arrayWithObjects:helloButton,btnSpace,doneButton,nil];    
  14.   
  15.     [doneButton release];    
  16.   
  17.     [btnSpace release];    
  18.   
  19.     [helloButton release];    
  20.   
  21.     [topView setItems:buttonsArray];    
  22.   
  23.     [tvTextView setInputAccessoryView:topView];    
  24.   
  25. -(IBAction)dismissKeyBoard    
  26.   
  27. {    
  28.   
  29.     [tvTextView resignFirstResponder];    
  30.   
  31. }    

 

 

(4)設定UITextView圓角問題

做法是在 #import QuartzCore/QuartzCore.h 後,便能調用[textView.layer setCornerRadius:10]; 來把 UITextView 設定圓角

(5)UITextView根據文字大小自適應高度

通過實現文本字數來確定高度,如下:

 

[csharp] view plaincopy 
  1. 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!";    
  2.   
  3. CGSize  size = [desc sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(240, 2000) lineBreakMode:UILineBreakModeWordWrap];   




只有UILabel需要定義的numberoflines為0,即不做行數的限制。如下:

 

[csharp] view plaincopy 
  1. [label  setNumberOfLines:0];    
  2. [label  setFrame:CGRectMake(40, 135, 240, size.height+10)];    
  3. [label setText:desc];   



 




 

(6)UITextView自定選擇文字後的菜單在ViewDidLoad中加入:

 

[csharp] view plaincopy 
  1. UIMenuItem *menuItem = [[UIMenuItem alloc]initWithTitle:@"分享到新浪微博" action:@selector(changeColor:)];  
  2. UIMenuController *menu = [UIMenuController sharedMenuController];  
  3. [menu setMenuItems:[NSArray arrayWithObject:menuItem]];  
  4. [menuItem release];  



 

當然上面那個@selector裡面的changeColor方法還是自己寫吧,也就是說點擊了我們自訂的功能表項目後會觸發的方法。

然後還得在代碼裡加上一個方法:

 

[csharp] view plaincopy 
  1. -(BOOL)canPerformAction:(SEL)action withSender:(id)sender  
  2. {  
  3. if(action [email protected](changeColor:))  
  4. {  
  5. if(textView.selectedRange.length>0)  
  6. return YES;  
  7. }  
  8. return NO;  
  9. }  



 

 

實現後如:

 

 

今天的UITextView就講到這裡,主要講了UITextView的初始化和開發中會遇到的一些問題和自訂等問題。謝謝大家支援哈。

更多:

http://www.cnblogs.com/likwo/archive/2010/05/29/1747117.html 

http://www.cnblogs.com/nasa/archive/2010/10/14/1851505.html 

http://www.cnblogs.com/otionsky/archive/2011/11/18/ios_disable_cut_copy_paste_replace.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.