#import <UIKit/UIKit.h>
@interface BRPlaceholderTextView : UITextView
@property(copy,nonatomic) NSString *placeholder;
@property(strong,nonatomic) NSIndexPath * indexPath;
@property(assign,nonatomic) NSInteger maxTextLength;
@property(assign,nonatomic) float updateHeight;
/**
* 增加text 长度限制
*
* @param maxLength <#maxLength description#>
* @param limit <#limit description#>
*/
-(void)addMaxTextLengthWithMaxLength:(NSInteger)maxLength andEvent:(void(^)(BRPlaceholderTextView*text))limit;
/**
* 开始编辑 的 回调
*
* @param begin <#begin description#>
*/
-(void)addTextViewBeginEvent:(void(^)(BRPlaceholderTextView*text))begin;
/**
* 结束编辑 的 回调
*
* @param begin <#begin description#>
*/
-(void)addTextViewEndEvent:(void(^)(BRPlaceholderTextView*text))End;
/**
* 设置Placeholder 颜色
*
* @param color <#color description#>
*/
-(void)setPlaceholderColor:(UIColor*)color;
/**
* 设置Placeholder 字体
*
* @param font <#font description#>
*/
-(void)setPlaceholderFont:(UIFont*)font;
/**
* 设置透明度
*
* @param opacity <#opacity description#>
*/
-(void)setPlaceholderOpacity:(float)opacity;
@end
#import "BRPlaceholderTextView.h"
#define kTopY 7.0
#define kLeftX 5.0
@interface BRPlaceholderTextView()<UITextViewDelegate>
@property(strong,nonatomic) UIColor *placeholder_color;
@property(strong,nonatomic) UIFont * placeholder_font;
@property(strong,nonatomic,readonly) UILabel *PlaceholderLabel;
@property(assign,nonatomic) float placeholdeWidth;
@property(copy,nonatomic) id eventBlock;
@property(copy,nonatomic) id BeginBlock;
@property(copy,nonatomic) id EndBlock;
@end
@implementation BRPlaceholderTextView
#pragma mark - life cycle
- (id) initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self awakeFromNib];
}
return self;
}
- (void)awakeFromNib {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DidChange:) name:UITextViewTextDidChangeNotification object:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewBeginNoti:) name:UITextViewTextDidBeginEditingNotification object:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewEndNoti:) name:UITextViewTextDidEndEditingNotification object:self];
float left=kLeftX,top=kTopY,hegiht=30;
self.placeholdeWidth=CGRectGetWidth(self.frame)-2*left;
_PlaceholderLabel=[[UILabel alloc] initWithFrame:CGRectMake(left, top
, _placeholdeWidth, hegiht)];
_PlaceholderLabel.numberOfLines=0;
_PlaceholderLabel.lineBreakMode=NSLineBreakByCharWrapping|NSLineBreakByWordWrapping;
[self addSubview:_PlaceholderLabel];
[self defaultConfig];
}
-(void)layoutSubviews
{
float left=kLeftX,top=kTopY,hegiht=self.bounds.size.height;
self.placeholdeWidth=CGRectGetWidth(self.frame)-2*left;
CGRect frame=_PlaceholderLabel.frame;
frame.origin.x=left;
frame.origin.y=top;
frame.size.height=hegiht;
frame.size.width=self.placeholdeWidth;
_PlaceholderLabel.frame=frame;
[_PlaceholderLabel sizeToFit];
}
-(void)dealloc{
[_PlaceholderLabel removeFromSuperview];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - System Delegate
#pragma mark - custom Delegate
#pragma mark - Event response
-(void)defaultConfig
{
self.placeholder_color = [UIColor lightGrayColor];
self.placeholder_font = [UIFont systemFontOfSize:15];
self.maxTextLength=1000;
self.layoutManager.allowsNonContiguousLayout=NO;
}
-(void)addMaxTextLengthWithMaxLength:(NSInteger)maxLength andEvent:(void (^)(BRPlaceholderTextView *text))limit
{
if (maxLength>0) {
_maxTextLength=maxLength;
}
if (limit) {
_eventBlock=limit;
}
}
-(void)addTextViewBeginEvent:(void (^)(BRPlaceholderTextView *))begin{
_BeginBlock=begin;
}
-(void)addTextViewEndEvent:(void (^)(BRPlaceholderTextView *))End{
_EndBlock=End;
}
-(void)setUpdateHeight:(float)updateHeight{
CGRect frame=self.frame;
frame.size.height=updateHeight;
self.frame=frame;
_updateHeight=updateHeight;
}
-(void)setPlaceholderFont:(UIFont *)font
{
self.placeholder_font=font;
}
-(void)setPlaceholderColor:(UIColor *)color
{
self.placeholder_color=color;
}
-(void)setPlaceholderOpacity:(float)opacity
{
if (opacity<0) {
opacity=1;
}
self.PlaceholderLabel.layer.opacity=opacity;
}
#pragma mark - Noti Event
-(void)textViewBeginNoti:(NSNotification*)noti{
if (_BeginBlock) {
void(^begin)(BRPlaceholderTextView*text)=_BeginBlock;
begin(self);
}
}
-(void)textViewEndNoti:(NSNotification*)noti{
if (_EndBlock) {
void(^end)(BRPlaceholderTextView*text)=_EndBlock;
end(self);
}
}
-(void)DidChange:(NSNotification*)noti{
if (self.placeholder.length == 0 || [self.placeholder isEqualToString:@""]) {
_PlaceholderLabel.hidden=YES;
}
if (self.text.length > 0) {
_PlaceholderLabel.hidden=YES;
}
else{
_PlaceholderLabel.hidden=NO;
}
NSString *lang = [[self.nextResponder textInputMode] primaryLanguage];
if ([lang isEqualToString:@"zh-Hans"]) {
UITextRange *selectedRange = [self markedTextRange];
UITextPosition *position = [self positionFromPosition:selectedRange.start offset:0];
if (!position) {
if (self.text.length > self.maxTextLength) {
self.text = [self.text substringToIndex:self.maxTextLength];
}
}
else{
}
}
else{
if (self.text.length > self.maxTextLength) {
self.text = [ self.text substringToIndex:self.maxTextLength];
}
}
if (_eventBlock && self.text.length > self.maxTextLength) {
void (^limint)(BRPlaceholderTextView*text) =_eventBlock;
limint(self);
}
}
#pragma mark - private method
+(float)boundingRectWithSize:(CGSize)size withLabel:(NSString *)label withFont:(UIFont *)font{
NSDictionary *attribute = @{NSFontAttributeName:font};
CGSize retSize = [label boundingRectWithSize:size
options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
attributes:attribute
context:nil].size;
return retSize.height;
}
#pragma mark - getters and Setters
-(void)setText:(NSString *)tex{
if (tex.length>0) {
_PlaceholderLabel.hidden=YES;
}
[super setText:tex];
}
-(void)setPlaceholder:(NSString *)placeholder{
if (placeholder.length == 0 || [placeholder isEqualToString:@""]) {
_PlaceholderLabel.hidden=YES;
}
else
{
_PlaceholderLabel.text=placeholder;
_placeholder=placeholder;
}
}
-(void)setPlaceholder_font:(UIFont *)placeholder_font
{
_placeholder_font=placeholder_font;
_PlaceholderLabel.font=placeholder_font;
}
-(void)setPlaceholder_color:(UIColor *)placeholder_color
{
_placeholder_color=placeholder_color;
_PlaceholderLabel.textColor=placeholder_color;
}