UITextField与UItextView

UITextField:不能换行

UITextView:没有提示文字,placeHolder

 

解决办法:

在UITextview中设置一个label,封装起来

 

 

//  Created by 殷婷婷 on 15-5-31.

//  Copyright (c) 2015年 lanou. All rights reserved.

//textView封装

 

#import "SendStatusView.h"

 

@implementation SendStatusView

 

- (instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {

        

        [self setUpSubviews];

        

    }

    return self;

}

 

//添加子控件

- (void)setUpSubviews{

    UILabel *label = [[UILabel alloc]init];

    label.textColor = [UIColor grayColor];

    //label.font = [UIFont systemFontOfSize:12.0];

    label.numberOfLines = 0;

    [self addSubview:label];

    self.placeholderLabel = label;

    

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:self];

}

 

- (void)dealloc{

    [[NSNotificationCenter defaultCenter]removeObserver:self];

}

- (void)textChange{

    self.placeholderLabel.hidden = self.text.length;

}

- (void)setPlaceHolder:(NSString *)placeHolder{

    

    if(placeHolder.length){

    CGSize maxSize = CGSizeMake(self.bounds.size.width - 10, MAXFLOAT);

    CGSize realSize = [placeHolder boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.font} context:nil].size;

    CGFloat labelX = 5;

    CGFloat labelY = 7;

    self.placeholderLabel.frame = CGRectMake(labelX, labelY, realSize.width, realSize.height);

        

        

        self.placeholderLabel.text = placeHolder;

 

    }

    

    

    

}

 

-(void)setFont:(UIFont *)font{

    

    [super setFont:font];

    

    self.placeholderLabel.font = font;

    

}

@end

 

你可能感兴趣的:(UITextField)