uitextview的佔位字

佔坑atom

 

 

#import <UIKit/UIKit.h>code

 

@interface LRFeedBackTextView : IQTextViewserver

 

/**placeholder佔位文字*/rem

@property (nonatomic, copy) NSString *placeholder;it

/**placeholderColor佔位文字顏色*/io

@property (nonatomic, strong) UIColor *placeholderColor;import

 

@endobject

 

 

 

#import "LRFeedBackTextView.h"select

@interface LRFeedBackTextView()scroll

 

/**UILabel*/

@property (nonatomic, strong) UILabel *placeholderLabel;

@end

 

@implementation LRFeedBackTextView

 

 

/**

 *  懶加載屬性,並設置屬性的值

 */

-(UILabel *)placeholderLabel

{

    if (!_placeholderLabel) {

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

        label.font = [UIFont systemFontOfSize:14];

        label.textColor = [UIColor grayColor];

        label.numberOfLines = 0;

        [self addSubview:label];

        _placeholderLabel = label;

    }

    return _placeholderLabel;

}

 

/**

 *  設置本身的屬性

 */

-(instancetype)initWithFrame:(CGRect)frame

{

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

        self.alwaysBounceVertical = YES;

        self.textColor = [UIColor blackColor];

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

        

    }

    return self;

}

 

- (void)awakeFromNib{

 

}

 

- (instancetype)initWithCoder:(NSCoder *)aDecoder{

    if (self = [super initWithCoder:aDecoder]) {

        self.scrollEnabled = NO;

        self.alwaysBounceVertical = YES;

        self.textColor = [UIColor blackColor];

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

    }

    return self;

}

 

- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

 

/**

 *  監聽有文字輸入

 */

-(void)texting

{

    [self setPlaceholderTextShow];

}

/**

 *  設置佔位文字的顯示

 */

-(void)setPlaceholderTextShow

{

    self.placeholderLabel.hidden = self.hasText;

}

 

-(void)layoutSubviews

{

    [super layoutSubviews];

    

    self.placeholderLabel.frame = CGRectMake(4, 8, SCREEN_WIDTH - 16, 0);

 

    [self.placeholderLabel sizeToFit];//這一步很重要,不能遺忘

}

 

-(void)setPlaceholder:(NSString *)placeholder

{

    placeholder = placeholder;

    self.placeholderLabel.text = placeholder;

    [self setNeedsLayout];

}

 

-(void)setPlaceholderColor:(UIColor *)placeholderColor

{

    self.placeholderLabel.textColor = placeholderColor;

    [self setNeedsLayout];

}

 

-(void)setFont:(UIFont *)font

{

    [super setFont:font];

    self.placeholderLabel.font = font;

    [self setNeedsLayout];

}

 

-(void)setText:(NSString *)text

{

    [super setText:text];

    [self setPlaceholderTextShow];

}

 

-(void)setAttributedText:(NSAttributedString *)attributedText

{

    [super setAttributedText:attributedText];

    [self setPlaceholderTextShow];

}

相關文章
相關標籤/搜索