//
// STPlaceHolderTextView.h
// yunxinDemo
//
// Created by 刘小二 on 16/7/13.
// Copyright © 2016年 刘小二. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface STPlaceHolderTextView : UITextView
/** 占位文字 */
@property (nonatomic, strong) NSString *placeholder;
/** 占位文字字体大小 */
@property (nonatomic, assign) CGFloat placeholderFont;
/** 占位文字颜色 */
@property (nonatomic, strong) UIColor *placeholderColor;
@end
```objc
//
// STPlaceHolderTextView.m
// yunxinDemo
//
// Created by 刘小二 on 16/7/13.
// Copyright © 2016年 刘小二. All rights reserved.
//
#import "STPlaceHolderTextView.h"
static CGFloat const STplaceholderDefaultFont = 13.0; // 默认占位文字大小
#define STplaceholderDefaultColor [UIColor grayColor] // 默认占位文字颜色
static CGFloat const STplaceholderDefaultInsets = 5.0; // 默认占位文字的间隙
@implementation STPlaceHolderTextView
- (void)setPlaceholder:(NSString *)placeholder {
if ([placeholder isEqualToString:_placeholder]) {
return;
}
_placeholder = placeholder;
[self setNeedsDisplay];
}
- (void)setPlaceholderFont:(CGFloat)placeholderFont {
if (placeholderFont == _placeholderFont) {
return;
}
_placeholderFont = placeholderFont;
[self setNeedsDisplay];
}
- (void)setPlaceholderColor:(UIColor *)placeholderColor {
if (placeholderColor == _placeholderColor) {
return;
}
_placeholderColor = placeholderColor;
[self setNeedsDisplay];
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
[self setNeedsDisplay];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChange:) name: UITextViewTextDidChangeNotification object:nil];
}
return self;
}
- (void)setText:(NSString *)text {
[super setText:text];
[self setNeedsDisplay];
}
- (void)setAttributedText:(NSAttributedString *)attributedText {
[super setAttributedText:attributedText];
[self setNeedsDisplay];
}
- (void)setFrame:(CGRect)frame{
if (self.frame.size.width != frame.size.width) {
[self setNeedsDisplay];
}
[super setFrame:frame];
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.placeholderFont = STplaceholderDefaultFont;
self.placeholderColor = STplaceholderDefaultColor;
[self setNeedsDisplay];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChange:) name: UITextViewTextDidChangeNotification object:self];
}
return self;
}
- (void)textViewDidChange:(NSNotification*)object {
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
if (self.text.length == 0 && self.placeholder) {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
paragraphStyle.alignment = self.textAlignment;
[self.placeholder drawInRect:CGRectMake(STplaceholderDefaultInsets, STplaceholderDefaultInsets, rect.size.width, rect.size.height) withAttributes:
@{
NSFontAttributeName:[UIFont systemFontOfSize:self.placeholderFont == STplaceholderDefaultFont ? STplaceholderDefaultFont: self.placeholderFont],
NSForegroundColorAttributeName:self.placeholderColor == STplaceholderDefaultColor ? STplaceholderDefaultColor:self.placeholderColor,
NSParagraphStyleAttributeName:paragraphStyle
}];
}
}
- (void)dealloc {
_placeholder = nil;
[[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidChangeNotification object:self];
}
@end