From 21d3023a9b7b6aff68c1170e345951396b1c6cfd Mon Sep 17 00:00:00 2001 From: 单军华 Date: Tue, 31 Jul 2018 13:35:21 +0800 Subject: [PATCH] no message --- screendisplay/screendisplay/Classes/Category/ThirdCategory/UITextView+WZB.m | 337 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 337 insertions(+), 0 deletions(-) diff --git a/screendisplay/screendisplay/Classes/Category/ThirdCategory/UITextView+WZB.m b/screendisplay/screendisplay/Classes/Category/ThirdCategory/UITextView+WZB.m new file mode 100755 index 0000000..fbfaa6a --- /dev/null +++ b/screendisplay/screendisplay/Classes/Category/ThirdCategory/UITextView+WZB.m @@ -0,0 +1,337 @@ +// +// UITextView+WZB.m +// WZBTextView-demo +// +// Created by normal on 2016/11/14. +// Copyright �� 2016��� WZB. All rights reserved. +// + +#import "UITextView+WZB.h" +#import <objc/runtime.h> + +// ������������ +static const void *WZBPlaceholderViewKey = &WZBPlaceholderViewKey; +// ������������������ +static const void *WZBPlaceholderColorKey = &WZBPlaceholderColorKey; +// ������������ +static const void *WZBTextViewMaxHeightKey = &WZBTextViewMaxHeightKey; +// ������������ +static const void *WZBTextViewMinHeightKey = &WZBTextViewMinHeightKey; +// ���������������block +static const void *WZBTextViewHeightDidChangedBlockKey = &WZBTextViewHeightDidChangedBlockKey; +// ��������������������� +static const void *WZBTextViewImageArrayKey = &WZBTextViewImageArrayKey; +// ��������������������������������������� +static const void *WZBTextViewLastHeightKey = &WZBTextViewLastHeightKey; + +@interface UITextView () + +// ��������������������� +@property (nonatomic, strong) NSMutableArray *wzb_imageArray; +// ��������������������������������������� +@property (nonatomic, assign) CGFloat lastHeight; + +@end + +@implementation UITextView (WZB) + +#pragma mark - Swizzle Dealloc ++ (void)load { + // ������dealoc + Method dealoc = class_getInstanceMethod(self.class, NSSelectorFromString(@"dealloc")); + Method myDealloc = class_getInstanceMethod(self.class, @selector(myDealloc)); + method_exchangeImplementations(dealoc, myDealloc); +} + +- (void)myDealloc { + // ������������ + [[NSNotificationCenter defaultCenter] removeObserver:self]; + + UITextView *placeholderView = objc_getAssociatedObject(self, WZBPlaceholderViewKey); + + // ������������������������������������������ + if (placeholderView) { + NSArray *propertys = @[@"frame", @"bounds", @"font", @"text", @"textAlignment", @"textContainerInset"]; + for (NSString *property in propertys) { + @try { + [self removeObserver:self forKeyPath:property]; + } @catch (NSException *exception) {} + } + } + [self myDealloc]; +} + +#pragma mark - set && get +- (UITextView *)wzb_placeholderView { + + // ������������������������textView���������������������������������������������������UITextView + UITextView *placeholderView = objc_getAssociatedObject(self, WZBPlaceholderViewKey); + + if (!placeholderView) { + + // ��������������� + self.wzb_imageArray = [NSMutableArray array]; + + placeholderView = [[UITextView alloc] init]; + // ������������������������������: ������������������������������������������ + objc_setAssociatedObject(self, WZBPlaceholderViewKey, placeholderView, OBJC_ASSOCIATION_RETAIN_NONATOMIC); + placeholderView = placeholderView; + + // ������������������ + placeholderView.scrollEnabled = placeholderView.userInteractionEnabled = NO; +// self.scrollEnabled = placeholderView.scrollEnabled = placeholderView.showsHorizontalScrollIndicator = placeholderView.showsVerticalScrollIndicator = placeholderView.userInteractionEnabled = NO; + placeholderView.textColor = [UIColor lightGrayColor]; + placeholderView.backgroundColor = [UIColor clearColor]; + [self refreshPlaceholderView]; + [self addSubview:placeholderView]; + + // ������������������ + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewTextChange) name:UITextViewTextDidChangeNotification object:self]; + + // ���������������������������������������������������������������������������TextDidChange������������������������text���������������������������������setText��� + NSArray *propertys = @[@"frame", @"bounds", @"font", @"text", @"textAlignment", @"textContainerInset"]; + + // ������������ + for (NSString *property in propertys) { + [self addObserver:self forKeyPath:property options:NSKeyValueObservingOptionNew context:nil]; + } + + } + return placeholderView; +} + +- (void)setWzb_placeholder:(NSString *)placeholder +{ + // ���placeholder������ + [self wzb_placeholderView].text = placeholder; +} + +- (NSString *)wzb_placeholder +{ + // ���������placeholder��������������������������������� + if (self.placeholderExist) { + return [self wzb_placeholderView].text; + } + return nil; +} + +- (void)setWzb_placeholderColor:(UIColor *)wzb_placeholderColor +{ + // ���������placeholder��������������������������������� + if (!self.placeholderExist) { + NSLog(@"������������placeholder������"); + } else { + self.wzb_placeholderView.textColor = wzb_placeholderColor; + + // ������������������������������: ������������������������������������������ + objc_setAssociatedObject(self, WZBPlaceholderColorKey, wzb_placeholderColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC); + } +} + +- (UIColor *)wzb_placeholderColor +{ + return objc_getAssociatedObject(self, WZBPlaceholderColorKey); +} + +- (void)setWzb_maxHeight:(CGFloat)wzb_maxHeight +{ + CGFloat max = wzb_maxHeight; + + // ���������������������������������textView������������������������������������������������������ + if (wzb_maxHeight < self.frame.size.height) { + max = self.frame.size.height; + } + + objc_setAssociatedObject(self, WZBTextViewMaxHeightKey, [NSString stringWithFormat:@"%lf", max], OBJC_ASSOCIATION_COPY_NONATOMIC); +} + +- (CGFloat)wzb_maxHeight +{ + return [objc_getAssociatedObject(self, WZBTextViewMaxHeightKey) doubleValue]; +} + +- (void)setWzb_minHeight:(CGFloat)wzb_minHeight +{ + objc_setAssociatedObject(self, WZBTextViewMinHeightKey, [NSString stringWithFormat:@"%lf", wzb_minHeight], OBJC_ASSOCIATION_COPY_NONATOMIC); +} + +- (CGFloat)wzb_minHeight +{ + return [objc_getAssociatedObject(self, WZBTextViewMinHeightKey) doubleValue]; +} + +- (void)setWzb_textViewHeightDidChanged:(textViewHeightDidChangedBlock)wzb_textViewHeightDidChanged +{ + objc_setAssociatedObject(self, WZBTextViewHeightDidChangedBlockKey, wzb_textViewHeightDidChanged, OBJC_ASSOCIATION_COPY_NONATOMIC); +} + +- (textViewHeightDidChangedBlock)wzb_textViewHeightDidChanged +{ + void(^textViewHeightDidChanged)(CGFloat currentHeight) = objc_getAssociatedObject(self, WZBTextViewHeightDidChangedBlockKey); + return textViewHeightDidChanged; +} + +- (NSArray *)wzb_getImages +{ + return self.wzb_imageArray; +} + +- (void)setLastHeight:(CGFloat)lastHeight { + objc_setAssociatedObject(self, WZBTextViewLastHeightKey, [NSString stringWithFormat:@"%lf", lastHeight], OBJC_ASSOCIATION_COPY_NONATOMIC); +} + +- (CGFloat)lastHeight { + return [objc_getAssociatedObject(self, WZBTextViewLastHeightKey) doubleValue]; +} + +- (void)setWzb_imageArray:(NSMutableArray *)wzb_imageArray { + objc_setAssociatedObject(self, WZBTextViewImageArrayKey, wzb_imageArray, OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + +- (NSMutableArray *)wzb_imageArray { + return objc_getAssociatedObject(self, WZBTextViewImageArrayKey); +} + +- (void)wzb_autoHeightWithMaxHeight:(CGFloat)maxHeight +{ + [self wzb_autoHeightWithMaxHeight:maxHeight textViewHeightDidChanged:nil]; +} +// ������������������������������������NO +static bool autoHeight = NO; +- (void)wzb_autoHeightWithMaxHeight:(CGFloat)maxHeight textViewHeightDidChanged:(textViewHeightDidChangedBlock)textViewHeightDidChanged +{ + autoHeight = YES; + [self wzb_placeholderView]; + self.wzb_maxHeight = maxHeight; + if (textViewHeightDidChanged) self.wzb_textViewHeightDidChanged = textViewHeightDidChanged; +} + +#pragma mark - addImage +/* ������������������ */ +- (void)wzb_addImage:(UIImage *)image +{ + [self wzb_addImage:image size:CGSizeZero]; +} + +/* ������������������ image:������������������ size:������������ */ +- (void)wzb_addImage:(UIImage *)image size:(CGSize)size +{ + [self wzb_insertImage:image size:size index:self.attributedText.length > 0 ? self.attributedText.length : 0]; +} + +/* ������������������ image:������������������ size:������������ index:��������������� */ +- (void)wzb_insertImage:(UIImage *)image size:(CGSize)size index:(NSInteger)index +{ + [self wzb_addImage:image size:size index:index multiple:-1]; +} + +/* ������������������ image:������������������ multiple:������������������������ */ +- (void)wzb_addImage:(UIImage *)image multiple:(CGFloat)multiple +{ + [self wzb_addImage:image size:CGSizeZero index:self.attributedText.length > 0 ? self.attributedText.length : 0 multiple:multiple]; +} + +/* ������������������ image:������������������ multiple:������������������������ index:��������������� */ +- (void)wzb_insertImage:(UIImage *)image multiple:(CGFloat)multiple index:(NSInteger)index +{ + [self wzb_addImage:image size:CGSizeZero index:index multiple:multiple]; +} + +/* ������������������ image:������������������ size:������������ index:��������������� multiple:������������������������ */ +- (void)wzb_addImage:(UIImage *)image size:(CGSize)size index:(NSInteger)index multiple:(CGFloat)multiple { + if (image) [self.wzb_imageArray addObject:image]; + NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText]; + NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init]; + textAttachment.image = image; + CGRect bounds = textAttachment.bounds; + if (!CGSizeEqualToSize(size, CGSizeZero)) { + bounds.size = size; + textAttachment.bounds = bounds; + } else if (multiple <= 0) { + CGFloat oldWidth = textAttachment.image.size.width; + CGFloat scaleFactor = oldWidth / (self.frame.size.width - 10); + textAttachment.image = [UIImage imageWithCGImage:textAttachment.image.CGImage scale:scaleFactor orientation:UIImageOrientationUp]; + } else { + bounds.size = image.size; + textAttachment.bounds = bounds; + } + + NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment]; + [attributedString replaceCharactersInRange:NSMakeRange(index, 0) withAttributedString:attrStringWithImage]; + self.attributedText = attributedString; + [self textViewTextChange]; + [self refreshPlaceholderView]; +} + + +#pragma mark - KVO������������������ +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { + [self refreshPlaceholderView]; + if ([keyPath isEqualToString:@"text"]) [self textViewTextChange]; +} + +// ������PlaceholderView +- (void)refreshPlaceholderView { + + UITextView *placeholderView = objc_getAssociatedObject(self, WZBPlaceholderViewKey); + + // ������������������������������������������ + if (placeholderView) { + self.wzb_placeholderView.frame = self.bounds; + if (self.wzb_maxHeight < self.bounds.size.height) self.wzb_maxHeight = self.bounds.size.height; + self.wzb_placeholderView.font = self.font; + self.wzb_placeholderView.textAlignment = self.textAlignment; + self.wzb_placeholderView.textContainerInset = self.textContainerInset; + self.wzb_placeholderView.hidden = (self.text.length > 0 && self.text); + } +} + +// ������������������ +- (void)textViewTextChange { + UITextView *placeholderView = objc_getAssociatedObject(self, WZBPlaceholderViewKey); + + // ������������������������������������������ + if (placeholderView) { + self.wzb_placeholderView.hidden = (self.text.length > 0 && self.text); + } + // ������������������������������������������������������ + if (!autoHeight) return; + if (self.wzb_maxHeight >= self.bounds.size.height) { + + // ������������ + NSInteger currentHeight = ceil([self sizeThatFits:CGSizeMake(self.bounds.size.width, MAXFLOAT)].height); + + // ������������������������������block + if (currentHeight != self.lastHeight) { + // ������������������ + self.scrollEnabled = currentHeight >= self.wzb_maxHeight; + CGFloat currentTextViewHeight = currentHeight >= self.wzb_maxHeight ? self.wzb_maxHeight : currentHeight; + // ������textView��������� + if (currentTextViewHeight >= self.wzb_minHeight) { + CGRect frame = self.frame; + frame.size.height = currentTextViewHeight; + self.frame = frame; + // ������block + if (self.wzb_textViewHeightDidChanged) self.wzb_textViewHeightDidChanged(currentTextViewHeight); + // ������������������ + self.lastHeight = currentTextViewHeight; + } + } + } + + if (!self.isFirstResponder) [self becomeFirstResponder]; +} + +// ���������������placeholder��������������������� +- (BOOL)placeholderExist { + + // ������������������������ + UITextView *placeholderView = objc_getAssociatedObject(self, WZBPlaceholderViewKey); + + // ���������placeholder��� + if (placeholderView) return YES; + + return NO; +} + +@end -- Gitblit v1.8.0