UITextView 和 UITextField 的提示信息placeholder

来源:互联网 发布:小女孩连裤袜淘宝 编辑:程序博客网 时间:2024/05/31 18:44

一、UITextFiled和UITextView很像,区别是前一个是只显示一行文本(即使打了回车,可只是显示一行),后一个可显示多行文本。两个的delegate方法也很像.

但是UITextFiled有一个属性placeholder,即文本框的提示信息。而UITextView则没有。一般当点击文本框的时候都需要将默认的提示信息去掉,将光标移动到开始位置。

对于UITextField 可以在delegate方法中这样设置:

 1 - (void) textFiledDidBeginEditing:(UITextFiled*)textFiled 2 { 3      textFiled.placholder = nil; 4      [textFiled becomeFirstResponser];    5 } 6  7 -(void)textFiledDidEndEditing:(UITextFiled*)textFiled 8 { 9        if(textFiled.text isEqualToString:@“”) || [textFiled.text stringBytrimmingCharactersInset:[NSCharacterSet  whitespaceAndNewlineCharacterSet]]length] == 010     {11           [textFiled setText:@""];12           textFiled.placeholder  = @"请您输入电话号码";        13     }          14     [textFiled resignFirstResponder];15 }    

 

但是对于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;@endSSTextView.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]

原创粉丝点击