UI交互做为与用户展示的核心内容,往往需要与设计密切相关,实际开发过程中,我们经常要用到alertview这种浸入式警告来展示一些核心内容以打断用户的操作。但是官方自带的UIAlertController自定义方面简直惨不忍睹。。。展示内容无法更加丰富,按钮样式无法高度自定义。。这些缺陷使的我们的UI开发与设计师的做法往往出现差异,使的产品不是那么完美 ,下面就给出参考实现:本实现支持MRC及ARC模式,放心使用。
项目中用到的一些类的扩展,如#import "NSExtentionSloter.h"可以在此博客中下载分拣使用:点击跳转后在博客的内容下方。
实现头文件:即新那建一个UIAlertViewController控制器,完全自己实现相应功能组件。
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIAlertViewController : UIViewController
@property (nonatomic,retain) NSString *viewTitle;
@property (nonatomic,retain) NSString *viewContent;
//点击空白区域是否消失,默认false
@property (nonatomic,assign) BOOL goneTouch;
//标题自定义
- (void)configTitleView:(void(^)(UIView *titleView))titleViewConfigBlock;
//显示内容自定义
- (void)configContentView:(void(^)(UIView *contentView))contentViewConfigBlock;
//Button自定义
- (void)configButtonView:(void(^)(UIView *buttonView))buttonViewConfigBlock;
//retrun bool,true自动隐藏 否则不隐藏
- (void)alertActionBlock:(BOOL (^)(NSInteger index))actionBlock;
//显示
- (void)showInViewController:(UIViewController *)pcontroller;
//消失
- (void)dismiss;
@end
NS_ASSUME_NONNULL_END
UIAlertViewController.m文件:
#define deviceHeight [UIScreen mainScreen].bounds.size.height
#define deviceWidth [UIScreen mainScreen].bounds.size.width
#import "UIAlertViewController.h"
#import "NSExtentionSloter.h"
typedef void (^titleViewConfigBlock)(UIView *aview);
typedef BOOL (^actionIndexBlock)(NSInteger index);
@interface UIAlertViewController ()
{
UIView *alertView;
}
@property (nonatomic,copy) titleViewConfigBlock titleConfigBlock;
@property (nonatomic,copy) titleViewConfigBlock contentConfigBlock;
@property (nonatomic,copy) titleViewConfigBlock buttonConfigBlock;
@property (nonatomic,copy) actionIndexBlock actionBlock;
@end
@implementation UIAlertViewController
@synthesize viewTitle,viewContent,titleConfigBlock,contentConfigBlock,actionBlock,buttonConfigBlock;
- (instancetype)init
{
self = [super init];
if (self)
{
[self setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self setModalPresentationStyle:UIModalPresentationOverCurrentContext];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, deviceWidth, deviceHeight);
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:frame];
[btn setBackgroundColor:[UIColor clearColor]];
[btn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
CGFloat ht = 160;
frame = CGRectMake(52, (deviceHeight-ht)/2, self.view.frame.size.width-104, ht);
alertView = [[UIView alloc]initWithFrame:frame];
[alertView setBackgroundColor:[UIColor whiteColor]];
[alertView.layer setMasksToBounds:YES];
[alertView.layer setCornerRadius:8];
[self.view addSubview:alertView];
frame = CGRectMake(0, 14, frame.size.width, 40);
if (self.titleConfigBlock)
{
UIView *tview = [[UIView alloc] initWithFrame:frame];
[alertView addSubview:tview];
self.titleConfigBlock(tview);
frame = tview.frame;
#if __has_feature(objc_arc)
#else
[tview release];
#endif
}
else
{
UILabel *label = [[UILabel alloc] initWithFrame:frame];
[label setBackgroundColor:[UIColor clearColor]];
[label setText:viewTitle?viewTitle:@"温馨提示"];
[label setTextAlignment:NSTextAlignmentCenter];
[label setFont:[UIFont boldSystemFontOfSize:19]];
[label setTextColor:[UIColor darkTextColor]];
[alertView addSubview:label];
#if __has_feature(objc_arc)
#else
[label release];
#endif
}
frame.origin.y += frame.size.height;
frame.size.height = ht - 54 - frame.origin.y;
if (self.contentConfigBlock)
{
UIView *cview = [[UIView alloc] initWithFrame:frame];
[alertView addSubview:cview];
self.contentConfigBlock(cview);
frame = cview.frame;
#if __has_feature(objc_arc)
#else
[cview release];
#endif
}
else
{
UILabel *label =[[UILabel alloc] initWithFrame:frame];
[label setBackgroundColor:[UIColor clearColor]];
[label setText:viewContent?viewContent:@""];
[label setTextAlignment:NSTextAlignmentCenter];
[label setFont:[UIFont systemFontOfSize:15]];
[label setTextColor:[UIColor blackColor]];
[alertView addSubview:label];
#if __has_feature(objc_arc)
#else
[label release];
#endif
}
ht = frame.origin.y+frame.size.height+54;
frame = alertView.frame;
frame.size.height = ht;
[alertView setFrame:frame];
CGRect rect = CGRectMake(0, ht-54, alertView.frame.size.width, 0.5);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect];
[imageView setBackgroundColor:RGB(232, 232, 232)];
[alertView addSubview:imageView];
#if __has_feature(objc_arc)
#else
[imageView release];
#endif
if (self.buttonConfigBlock)
{
CGRect fm = CGRectMake(0, ht-54, rect.size.width,54);
UIView *bview = [[UIView alloc] initWithFrame:fm];
[alertView addSubview:bview];
self.buttonConfigBlock(bview);
frame = bview.frame;
#if __has_feature(objc_arc)
#else
[bview release];
#endif
ht = frame.origin.y+frame.size.height;
frame = alertView.frame;
frame.size.height = ht;
[alertView setFrame:frame];
}
else
{
NSString *title = @"取消";
CGRect fm = CGRectMake(0, ht-54, rect.size.width/2,54);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:fm];
[button setTitle:title forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:19]];
[button setTitleColor:RGB(0, 122, 255) forState:UIControlStateNormal];
[button addTargetActionBlock:^(UIButton * _Nonnull aButton) {
if (self.actionBlock)
{
if (self.actionBlock(0))
{
[self dismiss];
}
}
}];
[alertView addSubview:button];
title = @"确定";
fm.origin.x += fm.size.width;
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:fm];
[button setTitle:title forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:19]];
[button setTitleColor:RGB(0, 122, 255) forState:UIControlStateNormal];
[button addTargetActionBlock:^(UIButton * _Nonnull aButton) {
if (self.actionBlock)
{
if (self.actionBlock(1))
{
[self dismiss];
}
}
}];
[alertView addSubview:button];
fm.size.width = 0.5;
fm.size.height = alertView.frame.size.height-fm.origin.y;
imageView = [[UIImageView alloc] initWithFrame:fm];
[imageView setBackgroundColor:RGB(232, 232, 232)];
[alertView addSubview:imageView];
#if __has_feature(objc_arc)
#else
[imageView release];
#endif
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.view setBackgroundColor:RGBA(0, 0, 0, 0.35)];
// CGRect frame = alertView.frame;
// BOOL NS_IPHONE_X = [[[UIApplication sharedApplication] delegate] window].safeAreaInsets.bottom>0;
// frame.origin.y = deviceHeight-frame.size.height-(NS_IPHONE_X?34:0);
// [UIView animateWithDuration:0.25 delay:0 options:0 animations:^
// {
// [self->alertView setFrame:frame];
// }
// completion:^(BOOL finished)
// {
//
// }];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
animation.duration = 0.3;
animation.removedOnCompletion = YES;
animation.fillMode = kCAFillModeForwards;
NSMutableArray *values = [NSMutableArray array];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
animation.values = values;
animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
[alertView.layer addAnimation:animation forKey:@"animationAlertKey"];
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)prefersStatusBarHidden
{
return YES;
}
- (void)dealloc
{
#if __has_feature(objc_arc)
#else
[viewTitle release];
[viewContent release];
[titleConfigBlock release];
[contentConfigBlock release];
[actionBlock release];
[buttonConfigBlock release];
[super dealloc];
#endif
}
#pragma mark - otherMethod
//标题自定义
- (void)configTitleView:(void(^)(UIView *titleView))block
{
[self setTitleConfigBlock:block];
}
//显示内容自定义
- (void)configContentView:(void(^)(UIView *contentView))block
{
[self setContentConfigBlock:block];
}
//Button自定义
- (void)configButtonView:(void(^)(UIView *buttonView))block
{
[self setButtonConfigBlock:block];
}
- (void)alertActionBlock:(BOOL (^)(NSInteger index))block
{
[self setActionBlock:block];
}
- (void)showInViewController:(UIViewController *)pcontroller
{
UIViewController *rootController = pcontroller.tabBarController?pcontroller.tabBarController:pcontroller;
[rootController presentViewController:self animated:YES completion:^
{
// self.view.backgroundColor = RGBA(0, 0, 0, 0.4);
}];
}
- (void)dismiss
{
[UIView animateWithDuration:0.25 animations:^{
self->alertView.transform=CGAffineTransformMakeScale(0.0001, 0.0001);
} completion:^(BOOL finished) {
}];
[self dismissViewControllerAnimated:YES completion:nil];
/*CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
animation.duration = 0.15;
animation.removedOnCompletion = YES;
animation.fillMode = kCAFillModeBackwards;
NSMutableArray *values = [NSMutableArray array];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.001, 0.001, 1.0)]];
animation.values = values;
animation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionLinear];
[alertView.layer addAnimation:animation forKey:@"animationAlertKey"];
[self dismissViewControllerAnimated:YES completion:nil];*/
}
- (void)cancelAction
{
if (self.goneTouch)
{
// CGRect frame = alertView.frame;
// frame.origin.y = deviceHeight;
// [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^
// {
// [self->alertView setFrame:frame];
// [self.view setBackgroundColor:RGBA(0, 0, 0, 0)];
// }
// completion:^(BOOL finished)
// {
// [self dismissViewControllerAnimated:YES completion:nil];
// }];
[UIView animateWithDuration:0.25 animations:^{
self->alertView.transform=CGAffineTransformMakeScale(0.01, 0.01);
} completion:^(BOOL finished) {
[self dismissViewControllerAnimated:YES completion:nil];
}];
}
}
@end
怎么使用呢?本类支持默认显示为系统样式的展示,也支持标题处、内容处、按钮处三处的自定义展示。
1、默认展示:
UIAlertViewController *controller = [[UIAlertViewController alloc] init];
[controller setTitle:@"温馨提示"];
[controller setViewContent:@"alert的提示内容区域"];
[controller alertActionBlock:^BOOL(NSInteger index) {
return YES;
}];
[controller showInViewController:self];
2、自定义展示:以下两例均为展示自定义使用方案
UIAlertViewController *toastView = [[UIAlertViewController alloc] init];
[toastView configTitleView:^(UIView * _Nonnull titleView) {
CGRect rt = titleView.frame;
rt.origin.y = 20;
rt.size.height = 24;
[titleView setFrame:rt];
UILabel *label = [[UILabel alloc] initWithFrame:titleView.bounds];
[label setText:@"通讯录权限未开启"];
[label setNumberOfLines:0];
[label setFont:[UIFont boldSystemFontOfSize:19]];
[label setTextColor:[UIColor blackColor]];
[label setTextAlignment:NSTextAlignmentCenter];
[titleView addSubview:label];
}];
[toastView configContentView:^(UIView * _Nonnull contentView) {
NSString *str = @"请将iphone“设置-隐私-通讯录”开关开启,并允许访问你的通讯录";
CGFloat ht = [str stringSizeWithFont:Font_size(15) constrainedSize:CGSizeMake(contentView.bounds.size.width-40, CGFLOAT_MAX)].height+30;
CGRect fm = contentView.frame;
fm.size.height = ht;
[contentView setFrame:fm];
fm.origin.y = 0;
fm.origin.x = 20;
fm.size.width -= 40;
fm.size.height = ht;
UILabel *label = [[UILabel alloc] initWithFrame:fm];
[label setText:str];
[label setNumberOfLines:0];
[label setFont:Font_size(15)];
[label setTextColor:RGB(40, 40, 40)];
[label setTextAlignment:NSTextAlignmentCenter];
[contentView addSubview:label];
}];
[toastView configButtonView:^(UIView * _Nonnull buttonView) {
CGFloat cntHeight = buttonView.frame.size.height;
CGFloat cntWidth = buttonView.frame.size.width;
NSString *title = @"取消";
CGRect fm = CGRectMake(0, 0, cntWidth/2,cntHeight);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:fm];
[button setTitle:title forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button addTargetActionBlock:^(UIButton * _Nonnull aButton) {
[toastView dismiss];
}];
[buttonView addSubview:button];
title = @"设置";
fm.origin.x += fm.size.width;
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:fm];
[button setTitle:title forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
[button setTitleColor:ZSMainBlue1 forState:UIControlStateNormal];
[button addTargetActionBlock:^(UIButton * _Nonnull aButton) {
NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
}
[toastView dismiss];
}];
[buttonView addSubview:button];
fm.size.width = 0.5;
fm.size.height = buttonView.frame.size.height-fm.origin.y;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:fm];
[imageView setBackgroundColor:RGB(235, 235, 235)];
[buttonView addSubview:imageView];
}];
[toastView showInViewController:self];
展示UI:
自定义展示2:带输入:
kWeakSelf(self)
UIAlertViewController *controller = [[UIAlertViewController alloc] init];
[controller configTitleView:^(UIView * _Nonnull titleView) {
CGRect fm = CGRectMake(10, 10, titleView.frame.size.width/2, 34);
UILabel *label = [[UILabel alloc] initWithFrame:fm];
[label setTextAlignment:NSTextAlignmentLeft];
[label setFont:Font_size_defalut_15];
[titleView addSubview:label];
NSMutableAttributedString *mtext = [[NSMutableAttributedString alloc] initWithString:@"*拒绝理由"];
[mtext addAttribute:NSForegroundColorAttributeName value:HexColor(@"#F77F6D") range:NSMakeRange(0, 1)];
[mtext addAttribute:NSForegroundColorAttributeName value:HexColor(@"#000000") range:NSMakeRange(1, mtext.length-1)];
[label setAttributedText:mtext];
fm.origin.x += fm.size.width;
fm.size.width = titleView.frame.size.width-fm.origin.x-20;
label = [[UILabel alloc] initWithFrame:fm];
[label setTextAlignment:NSTextAlignmentRight];
[label setFont:Font_size_defalut_13];
[label setTextColor:HexColor(@"#999999")];
[label setText:@"0/300"];
[titleView addSubview:label];
kStrongSelf(self)
[self setAlertCountLabel:label];
}];
[controller configContentView:^(UIView * _Nonnull contentView) {
CGRect fm = contentView.frame;
fm.size.height = 110;
[contentView setFrame:fm];
fm = CGRectMake(10, 10, contentView.bounds.size.width-20, fm.size.height-20);
self.alertTextView = [[UIMutableTextView alloc]initWithFrame:fm];
self.alertTextView.returnKeyType = UIReturnKeyDone;
// self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
self.alertTextView.font = [UIFont systemFontOfSize:14];
[self.alertTextView setPlaceholder:@" 请输入拒绝理由" constraintsBlock:nil];
self.alertTextView.textAlignment = NSTextAlignmentLeft;
[self.alertTextView addTextViewTextDidChange:^(UIMutableTextView * _Nonnull textView) {
kStrongSelf(self)
self.alertCountLabel.text = [NSString stringWithFormat:@"%lu/300",(unsigned long)textView.text.length];
if (textView.text.length > 300) {
textView.text = [textView.text substringToIndex:300];
self.alertCountLabel.text = @"300/300";
[MBProgressHUD showWindowMessageThenHide:@"最多输入300字"];
return;
}
}];
self.alertTextView.textColor = @"#333333".color;
[self.alertTextView.layer setMasksToBounds:YES];
[self.alertTextView.layer setCornerRadius:4];
[self.alertTextView.layer setBorderColor:HexColor(@"#E8E8E8").CGColor];
[self.alertTextView.layer setBorderWidth:0.5];
[contentView addSubview:self.alertTextView];
}];
[controller alertActionBlock:^BOOL(NSInteger index) {
if (index == 1)
{
kStrongSelf(self)
NSString *text = self.alertTextView.text;
if (text && text.length>0)
{
[self refuseAcion:text model:model];
return YES;
}
else
{
[MBProgressHUD showWindowMessageThenHide:@"请输入拒绝理由"];
return NO;
}
}
return YES;
}];
[controller showInViewController:self.containerController];
展示UI:
@interface UIMutableTextView : UITextView
- (void)setPlaceholder:(NSString *)placeholder constraintsBlock:(nullable void (^)(UILabel* _Nonnull placeLable))constraintsBlock;
- (void)setMutableAntributedTextPlaceholder:(NSMutableAttributedString *)attext constraintsBlock:(nullable void (^)(UILabel* _Nonnull placeLable))constraintsBlock;
- (void)setContentSize:(CGSize)contentSize;
- (void)setAutoTraceContent:(BOOL)flag;
- (void)listenFrameAndDispatchBlock:(void (^)(CGRect frame))block;
- (void)addTextViewTextDidChange:(void(^)(UIMutableTextView *textView))didChangedBlock;
@end
typedef void (^textViewListenBlock)(CGRect frame);
typedef void (^textViewTextDidChangedBlock)(UIMutableTextView *textView);
@interface UIMutableTextView ()
@property (nonatomic,retain) UILabel *placeLable;
@property (nonatomic,copy) textViewListenBlock listenBlock;
@property (nonatomic,copy) textViewTextDidChangedBlock changedBlock;
@property (nonatomic,assign) BOOL autoTraceContent;
@end
@implementation UIMutableTextView
@synthesize placeLable,listenBlock,autoTraceContent,changedBlock;
- (void)setPlaceholder:(NSString *)placeholder constraintsBlock:(nullable void (^)(UILabel * _Nonnull))constraintsBlock
{
if (!placeLable)
{
CGRect frame = CGRectMake(8, 5, self.bounds.size.width-10, CGFLOAT_MAX);
CGSize size = [placeholder stringSizeWithFont:[UIFont systemFontOfSize:14.0] constrainedSize:frame.size];
frame.size.height = MAX(25, size.height);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor grayColor]];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:14]];
[label setTextAlignment:NSTextAlignmentLeft];
[self addSubview:label];
[self setPlaceLable:label];
#if __has_feature(objc_arc)
#else
[label release];
#endif
}
[self.placeLable setText:placeholder];
// [[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewChangedText:) name:UITextViewTextDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewEndEditing:) name:UITextViewTextDidEndEditingNotification object:nil];
if (constraintsBlock)
{
constraintsBlock(self.placeLable);
}
}
- (void)setMutableAntributedTextPlaceholder:(NSMutableAttributedString *)attext constraintsBlock:(nullable void (^)(UILabel * _Nonnull))constraintsBlock
{
if (!placeLable)
{
NSRange range = NSMakeRange(0, attext.length);
UIFont *font = [attext attribute:NSFontAttributeName atIndex:0 effectiveRange:&range];
CGRect frame = CGRectMake(8, 5, self.bounds.size.width-10, font.lineHeight);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
[label setBackgroundColor:[UIColor clearColor]];
[label setNumberOfLines:0];
[label setTextAlignment:NSTextAlignmentRight];
[self addSubview:label];
[self setPlaceLable:label];
#if __has_feature(objc_arc)
#else
[label release];
#endif
}
[self.placeLable setAttributedText:attext];
// [[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewChangedText:) name:UITextViewTextDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewEndEditing:) name:UITextViewTextDidEndEditingNotification object:nil];
if (constraintsBlock)
{
constraintsBlock(self.placeLable);
}
}
- (void)drawRect:(CGRect)rect
{
NSString *text = [self text];
if (text&&text.length>0)
{
[placeLable setHidden:YES];
}
else
{
[placeLable setHidden:NO];
}
}
- (void)setText:(NSString *)text
{
[super setText:text];
if (text&&text.length>0)
{
[placeLable setHidden:YES];
}
else
{
[placeLable setHidden:NO];
}
}
- (void)textViewBeginEditing:(NSNotification*)notification
{
if (notification.object == self)
{
if (placeLable)
{
[placeLable setHidden:YES];
}
}
}
- (void)textViewEndEditing:(NSNotification*)notification
{
if (notification.object == self)
{
NSString *text = [self text];
if (!text || text.length == 0)
{
if (placeLable)
{
[placeLable setHidden:NO];
}
}
else
{
[placeLable setHidden:YES];
}
}
}
- (void)setAutoTraceContent:(BOOL)flag
{
autoTraceContent = flag;
}
- (void)addTextViewTextDidChange:(void(^)(UIMutableTextView *textView))didChangedBlock
{
[self setChangedBlock:didChangedBlock];
}
- (void)textViewChangedText:(NSNotification*)notification
{
if (notification.object == self)
{
NSString *text = [self text];
if (!text || text.length == 0)
{
if (placeLable)
{
[placeLable setHidden:NO];
}
}
else
{
[placeLable setHidden:YES];
}
if (self.changedBlock)
{
self.changedBlock(self);
}
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidBeginEditingNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidEndEditingNotification object:nil];
#if __has_feature(objc_arc)
#else
if (placeLable)
{
[placeLable release];
placeLable = nil;
}
[listenBlock release];
[changedBlock release];
[super dealloc];
#endif
}
- (void)setContentSize:(CGSize)contentSize
{
CGSize oriSize = self.contentSize;
[super setContentSize:contentSize];
if (autoTraceContent)
{
if(oriSize.height != self.contentSize.height)
{
CGRect newFrame = self.frame;
newFrame.size.height = self.contentSize.height;
self.frame = newFrame;
if (listenBlock)
{
listenBlock(newFrame);
}
}
}
}
- (void)listenFrameAndDispatchBlock:(textViewListenBlock)block
{
[self setListenBlock:block];
}
@end