1.建立並初始化 app
建立UITextView的文件,並在.h文件中寫入以下代碼: 字體
- #import <UIKit/UIKit.h>
-
- @interface TextViewController : UIViewController <UITextViewDelegate>
- {
- UITextView *textView;
- }
-
- @property (nonatomic, retain) UITextView *textView;
-
- @end
在.m文件中初始化這個textview,寫入代碼以下: atom
- self.textView = [[[UITextView alloc] initWithFrame:self.view.frame]autorelease]; //初始化大小並自動釋放
-
- self.textView.textColor = [UIColor blackColor];//設置textview裏面的字體顏色
-
- self.textView.font = [UIFont fontWithName:@"Arial" size:18.0];//設置字體名字和字體大小
-
- self.textView.delegate = self;//設置它的委託方法
-
- self.textView.backgroundColor = [UIColor whiteColor];//設置它的背景顏色
-
-
-
- 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.";//設置它顯示的內容
-
- self.textView.returnKeyType = UIReturnKeyDefault;//返回鍵的類型
-
- self.textView.keyboardType = UIKeyboardTypeDefault;//鍵盤類型
-
- self.textView.scrollEnabled = YES;//是否能夠拖動
-
-
-
- self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自適應高度
-
-
-
- [self.view addSubview: self.textView];//加入到整個頁面中
2. UITextView退出鍵盤的幾種方式 spa
由於你點擊UITextView會出現鍵盤,若是你退出鍵盤,有以下幾種方式: .net
(1)若是你程序是有導航條的,能夠在導航條上面加多一個Done的按鈕,用來退出鍵盤,固然要先實UITextViewDelegate。 blog
代碼以下: ip
- - (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裏不用回車鍵,能夠把回車鍵當作退出鍵盤的響應鍵。
代碼以下: get
- #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鍵均可以達到退出鍵盤的效果。 flash
(3)還有你也能夠自定義其餘加載鍵盤上面用來退出,好比在彈出的鍵盤上面加一個view來放置退出鍵盤的Done按鈕。 it
代碼以下:
- 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)設置UITextView圓角問題
作法是在 #import QuartzCore/QuartzCore.h 後,便能調用[textView.layer setCornerRadius:10]; 來把 UITextView 設定圓角
(5)UITextView根據文本大小自適應高度
經過實現文本字數來肯定高度,以下:
- 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,即不作行數的限制。以下:
- [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];
固然上面那個@selector裏面的changeColor方法仍是本身寫吧,也就是說點擊了咱們自定義的菜單項後會觸發的方法。
而後還得在代碼里加上一個方法:
2、UITextField
(1)初始化UITextField
- UITextField* text = [[UITextField alloc] initWithFrame:CGRectMake(10, 50, 300, 30)];
-
- text.borderStyle = UITextBorderStyleRoundedRect;
-
- text.autocorrectionType = UITextAutocorrectionTypeYes;
-
- text.placeholder = @"您好,我是Andy—清風";
-
- text.returnKeyType = UIReturnKeyDone;
-
- text.clearButtonMode = UITextFieldViewModeWhileEditing;
-
- [text setBackgroundColor:[UIColor whiteColor]];
-
- text.delegate = self;
-
- [self.view addSubview:text];
(2)詳細參數解釋
borderStyle:文本框的邊框風格
autocorrectionType:能夠設置是否啓動自動提醒更正功能。
placeholder:設置默認的文本顯示
returnKeyType:設置鍵盤完成的按鈕
backgroundColor:設置背景顏色
delegate:設置委託
(3)委託方法
- -(void)textFieldDidBeginEditing:(UITextField *)textField;
-
- //當開始點擊textField會調用的方法
-
-
-
- -(void)textFieldDidEndEditing:(UITextField *)textField;
-
- //當textField編輯結束時調用的方法
-
- //按下Done按鈕的調用方法,咱們讓鍵盤消失
-
- -(BOOL)textFieldShouldReturn:(UITextField *)textField{
-
- [textField resignFirstResponder];
-
- return YES;
-
- }