IOS6 控件專輯 – iPhone手機開發技術文章 iPhone軟體開發教學課程

IOS 控件專輯
UITextView控件

1. 創建並初始化

創建UITextView的文件,並在.h文件中寫入如下代碼:
//==============================================================================

#import <UIKit/UIKit.h>

@interface TextViewController : UIViewController <UITextViewDelegate>

@property (nonatomic, weak) UITextView *textView;

@end

//==============================================================================

在.m文件中初始化這個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 =@"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圓角問題

引用#import QuartzCore/QuartzCore.h後,
調用[textView.layer setCornerRadius:10];設定UITextView圓角

3. 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.0f]
       constrainedToSize:CGSizeMake(FLT_MAX,21.0)
   lineBreakMode:NSLineBreakByWordWrapping];

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

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

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

代碼如下:

– (void)textViewDidBeginEditing:(UITextView *)textView
{
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leaveEditMode)];

self.navigationItem.rightBarButtonItem = done;
}

– (void)textViewDidEndEditing:(UITextView *)textView
{
    self.navigationItem.rightBarButtonItem = nil;
}

– (void)leaveEditMode
{
    [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;
}

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

(3)還有你也可以自定義其他加載鍵盤上面用來退出,比如在彈出的鍵盤上面加一個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 www.aiwalls.com
 target:self action:@selector(dismissKeyBoard)];

NSArray * buttonsArray =@[helloButton, btnSpace, doneButton];

[topView setItems:buttonsArray];

[tvTextView setInputAccessoryView:topView];
}

-(void)dismissKeyBoard
{
    [tvTextView resignFirstResponder];
}

 

You May Also Like