創建UITextView的文件,並在.h文件中寫入如下代碼:
<span style="font-family:Times New Roman;font-size:14px;"><strong>#import <UIKit/UIKit.h>
@interface TextViewController : UIViewController <UITextViewDelegate>{
UITextView *textView;
}
@property (nonatomic, retain) UITextView *textView;
@end</strong></span>
在.m文件中初始化這個textview,寫入代碼如下
<span style="font-family:Times New Roman;font-size:14px;"><strong>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 to come to serve their country.\n\nNow is the time for all good developers to come to serve their country.";//設置它顯示的內容
self.textView.returnKeyType = UIReturnKeyDefault;//返回鍵的類型
self.textView.keyboardType = UIKeyboardTypeDefault;//鍵盤類型
self.textView.scrollEnabled = YES;//是否可以拖動
self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自適應高度
[self.view addSubview: self.textView];//加入到整個頁面中</strong></span>
2. UITextView退出鍵盤的幾種方式(1)如果你程序是有導航條的,可以在導航條上面加多一個Done的按鈕,用來退出鍵盤,當然要先實現UITextViewDelegate。
<span style="font-family:Times New Roman;font-size:14px;"><strong>- (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];
}</strong></span>
(2)如果你的textview裏不用回車鍵,可以把回車鍵當做退出鍵盤的響應鍵。
<span style="font-family:Times New Roman;"><strong><span style="font-size:14px;">#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;
}</span></strong></span>
(3)還有你也可以自定義其他視圖控件加載到鍵盤上用來退出,比如在彈出的鍵盤上面加一個view來放置退出鍵盤的Done按鈕。
<span style="font-family:Times New Roman;font-size:14px;"><strong>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];
}</strong></span>
3. UITextView根據內容自動調試高度
(1)使用sizeWithFont在編輯過程中調節高度
<span style="font-family:Times New Roman;color:#000000;">- (void)textViewDidChange:(UITextView *)textView{
if(textView.text.length > 20)//一行最多多少字節
{
//TextView底面背景圖片根據內容自動調整高度
UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@”inputbox”ofType:@”png”]];
[BgImage setImage:[img stretchableImageWithLeftCapWidth:21 topCapHeight:14]];
UIFont *font = [UIFont systemFontOfSize:12];
CGSize size = [textView.text sizeWithFont:font constrainedToSize:CGSizeMake(320, 140)lineBreakMode:UILineBreakModeWordWrap];
BgImage.frame = CGRectMake(0, 202-size.height+15, 320, size.height+28);
InputTextVeiw.contentInset = UIEdgeInsetsZero;//以換行爲基準
[textView setFrame:CGRectMake(51, 210-size.height+18, 200, size.height+5)];
}
}</span>
(2)使用boundingRectWithsizet在編輯過程中調節高度
-(void)textViewDidChange:(UITextView *)textView{
CGFloat height = [textView.text boundingRectWithSize:CGSizeMake(textView.frame.size.width,
<span style="font-family:Times New Roman;"> </span> CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|
<span style="font-family:Times New Roman;"> </span>NSStringDrawingUsesFontLeading attributes:[NSDictionary
<span style="font-family:Times New Roman;"> </span>dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:[textView.font pointSize]],
<span style="font-family:Times New Roman;"> </span>NSFontAttributeName, nil] context:nil].size.height;
textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y,
<span style="font-family:Times New Roman;"> </span>textView.frame.size.width, height);
}
4. UITextView根據設置行距
(1)如果只是靜態顯示textView的內容爲設置的行間距,執行如下代碼
(2)如果是想在輸入內容的時候就按照設置的行間距進行動態改變,那就需要將上面代碼放到textView的delegate方法裏
(1)如果只是靜態顯示textView的內容爲設置的行間距,執行如下代碼
<span style="font-family:Times New Roman;color:#000000;">// textview 改變字體的行間距
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10;// 字體的行間距
NSDictionary *attributes = @{
NSFontAttributeName:[UIFont systemFontOfSize:15],
NSParagraphStyleAttributeName:paragraphStyle
};
textView.attributedText = [[NSAttributedString alloc] initWithString:@"輸入你的內容" attributes:attr</span><span style="color:#000000;">ibutes];</span>
-(void)textViewDidChange:(UITextView *)textView
{
// textview 改變字體的行間距
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 20;// 字體的行間距
NSDictionary *attributes = @{
NSFontAttributeName:[UIFont systemFontOfSize:15],
NSParagraphStyleAttributeName:paragraphStyle
};
textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
}
4. UITextView設置Placeholder
(1)UITextView上如何加上類似於UITextField的placeholder呢,我們可以在UITextView上加一個UILabel或者一個UITextView,在適當的適合出現或隱藏就好
<span style="font-family:Times New Roman;font-size:14px;"><strong>- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{ if (![text isEqualToString:@""])
{
_placeholderLabel.hidden = YES;
}
if ([text isEqualToString:@""] && range.location == 0 && range.length == 1)
{
_placeholderLabel.hidden = NO;
}
return YES;
}</strong></span>
注意點: (1) _placeholderLabel 是加在UITextView後面的UITextView,_placeholderLabel要保證和真正的輸入框的設置一樣,字體設置成淺灰色,然後[_placeholderLabel setEditable:NO];真正的輸入框要設置背景色透明,保證能看到底部的_placeholderLabel。
(2) [text isEqualToString:@""] 表示輸入的是退格鍵
(3) range.location == 0 && range.length == 1 表示輸入的是第一個字符
或參考:點擊打開鏈接