一、UITextFiled和UITextView很像,區別是前一個是只顯示一行文本(即使打了回車,可只是顯示一行),后一個可顯示多行文本。兩個的delegate方法也很像.
但是UITextFiled有一個屬性placeholder,即文本框的提示信息。而UITextView則沒有。一般當點擊文本框的時候都需要將默認的提示信息去掉,將光標移動到開始位置。
但是對於UITextView則沒有placeholder這個屬性,可以直接設置textView.text = @"請您輸入電話號碼". 清除默認的text有幾種方法
1.在UITextView上添加一個UILabel,再在-(void)textviewDidChanged:(UITextView*)textView方法中移除掉這個Label,[label removeFromSuperView];
2.繼承UITextView,在drawRect中添加或者刪除placeholder: 參考http://stackoverflow.com/questions/1328638/placeholder-in-uitextview
SSTextView.h
//
// SSTextView.h
// SSToolkit
//
// Created by Sam Soffes on 8/18/10.
// Copyright 2010-2011 Sam Soffes. All rights reserved.
//
/**
UITextView subclass that adds placeholder support like UITextField has.
*/
@interface SSTextView : UITextView
/**
The string that is displayed when there is no other text in the text view.
The default value is `nil`.
*/
@property (nonatomic, retain) NSString *placeholder;
/**
The color of the placeholder.
The default is `[UIColor lightGrayColor]`.
*/
@property (nonatomic, retain) UIColor *placeholderColor;
@end
SSTextView.m
//
// SSTextView.m
// SSToolkit
//
// Created by Sam Soffes on 8/18/10.
// Copyright 2010-2011 Sam Soffes. All rights reserved.
//
#import "SSTextView.h"
@interface SSTextView ()
- (void)_initialize;
- (void)_updateShouldDrawPlaceholder;
- (void)_textChanged:(NSNotification *)notification;
@end
@implementation SSTextView {
BOOL _shouldDrawPlaceholder;
}
#pragma mark - Accessors
@synthesize placeholder = _placeholder;
@synthesize placeholderColor = _placeholderColor;
- (void)setText:(NSString *)string {
[super setText:string];
[self _updateShouldDrawPlaceholder];
}
- (void)setPlaceholder:(NSString *)string {
if ([string isEqual:_placeholder]) {
return;
}
[_placeholder release];
_placeholder = [string retain];
[self _updateShouldDrawPlaceholder];
}
#pragma mark - NSObject
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:self];
[_placeholder release];
[_placeholderColor release];
[super dealloc];
}
#pragma mark - UIView
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self _initialize];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self _initialize];
}
return self;
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
if (_shouldDrawPlaceholder) {
[_placeholderColor set];
[_placeholder drawInRect:CGRectMake(8.0f, 8.0f, self.frame.size.width - 16.0f, self.frame.size.height - 16.0f) withFont:self.font];
}
}
#pragma mark - Private
- (void)_initialize {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_textChanged:) name:UITextViewTextDidChangeNotification object:self];
self.placeholderColor = [UIColor colorWithWhite:0.702f alpha:1.0f];
_shouldDrawPlaceholder = NO;
}
- (void)_updateShouldDrawPlaceholder {
BOOL prev = _shouldDrawPlaceholder;
_shouldDrawPlaceholder = self.placeholder && self.placeholderColor && self.text.length == 0;
if (prev != _shouldDrawPlaceholder) {
[self setNeedsDisplay];
}
}
- (void)_textChanged:(NSNotification *)notificaiton {
[self _updateShouldDrawPlaceholder];
}
@end
3.還是使用UITextView的delegate方法
- (BOOL) textViewShouldBeginEditing:(UITextView *)textView
{
if(textView.tag == 0) {
textView.text = @"";
textView.textColor = [UIColor blackColor];
textView.tag = 1;
}
return YES;
}
- (void)textViewDidChange:(UITextView *)textView
{
if([textView.text length] == 0)
{
textView.text = @"Foobar placeholder";
textView.textColor = [UIColor lightGrayColor];
textView.tag = 0;
}
}
二、如果希望一進入Controller頁面,就顯示鍵盤,可以在viewWillAppear或者viewDidAppear中 添加[textFiled becomeFirstResponser]
