iOS開發中如何在UITextView中添加預設文字

來源:互聯網
上載者:User

標籤:

在UITextField中內建placeholder屬性,可以用於提示輸入框資訊。但是UITextView並不具備此功能
介紹兩種方法來實現:
第一種:
初始化UITextView
//首先定義UITextView  
UITextView *textView = [[UITextView alloc] init];  
textView.font = [UIFont systemFontOfSize:14];  
textView.frame =CGRectMake(10, 0, cell.contentView.bounds.size.width-20, side);  
textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;  
textView.backgroundColor = [UIColor whiteColor];  
[cell.contentView addSubview:textView];  
textView.hidden = NO;  
textView.delegate = self;  
//其次在UITextView上面覆蓋個UILable,UILable設定為全域變數。  
uilabel.frame =CGRectMake(17, 8, cell.contentView.bounds.size.width - side+10, 20);  
uilabel.text = @"請填寫審批意見...";  
uilabel.enabled = NO;//lable必須設定為不可用  
uilabel.backgroundColor = [UIColor clearColor];  
[cell.contentView addSubview:uilabel];  
實現UITextView的代理
-(void)textViewDidChange:(UITextView *)textView  
{  
self.examineText = textView.text;  
if (textView.text.length == 0) {  
uilabel.text = @"請填寫審批意見...";  
}else{  
uilabel.text = @"";  
}  


第二種:
UITextView 實現 placeholder 及隱藏鍵盤

#import <Foundation/Foundation.h>
@interface UIPlaceHolderTextView : UITextView {
NSString *placeholder;
UIColor *placeholderColor;

@private
UILabel *placeHolderLabel;
}

@property(nonatomic, retain) UILabel *placeHolderLabel;
@property(nonatomic, retain) NSString *placeholder;
@property(nonatomic, retain) UIColor *placeholderColor;

-(void)textChanged:(NSNotification*)notification;

@end

#import "UIPlaceHolderTextView.h"
@implementation UIPlaceHolderTextView
@synthesize placeHolderLabel;
@synthesize placeholder;
@synthesize placeholderColor;

- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[placeHolderLabel release]; placeHolderLabel = nil;
[placeholderColor release]; placeholderColor = nil;
[placeholder release]; placeholder = nil;
[super dealloc];
}

- (void)awakeFromNib
{
[super awakeFromNib];
[self setPlaceholder:@""];
[self setPlaceholderColor:[UIColor lightGrayColor]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
}

- (id)initWithFrame:(CGRect)frame
{
if( (self = [super initWithFrame:frame]) )
{
[self setPlaceholder:@""];
[self setPlaceholderColor:[UIColor lightGrayColor]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
}
return self;
}

- (void)textChanged:(NSNotification *)notification
{
if([[self placeholder] length] == 0)
{
return;
}

if([[self text] length] == 0)
{
[[self viewWithTag:999] setAlpha:1];
}
else
{
[[self viewWithTag:999] setAlpha:0];
}
}

- (void)setText:(NSString *)text {
[super setText:text];
[self textChanged:nil];
}

- (void)drawRect:(CGRect)rect
{
if( [[self placeholder] length] > 0 )
{
if ( placeHolderLabel == nil )
{
placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width - 16,0)];
placeHolderLabel.lineBreakMode = UILineBreakModeWordWrap;
placeHolderLabel.numberOfLines = 0;
placeHolderLabel.font = self.font;
placeHolderLabel.backgroundColor = [UIColor clearColor];
placeHolderLabel.textColor = self.placeholderColor;
placeHolderLabel.alpha = 0;
placeHolderLabel.tag = 999;
[self addSubview:placeHolderLabel];
}

placeHolderLabel.text = self.placeholder;
[placeHolderLabel sizeToFit];
[self sendSubviewToBack:placeHolderLabel];
}

if( [[self text] length] == 0 && [[self placeholder] length] > 0 )
{
[[self viewWithTag:999] setAlpha:1];
}

[super drawRect:rect];
}
@end

//隱藏鍵盤,實現UITextViewDelegate
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text  
{  
if ([text isEqualToString:@"\n"]) {  
[m_textView resignFirstResponder];  
return NO;  
}  
return YES;  
}

iOS開發中如何在UITextView中添加預設文字

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.