From 7b02207537d35bfa1714bf8beafc921f717d100a Mon Sep 17 00:00:00 2001 From: 单军华 Date: Wed, 11 Jul 2018 10:47:42 +0800 Subject: [PATCH] 首次上传 --- screendisplay/Pods/HMEmoticon/表情键盘/Emoticon/HMEmoticonTextView.m | 329 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 329 insertions(+), 0 deletions(-) diff --git "a/screendisplay/Pods/HMEmoticon/\350\241\250\346\203\205\351\224\256\347\233\230/Emoticon/HMEmoticonTextView.m" "b/screendisplay/Pods/HMEmoticon/\350\241\250\346\203\205\351\224\256\347\233\230/Emoticon/HMEmoticonTextView.m" new file mode 100755 index 0000000..d25cf82 --- /dev/null +++ "b/screendisplay/Pods/HMEmoticon/\350\241\250\346\203\205\351\224\256\347\233\230/Emoticon/HMEmoticonTextView.m" @@ -0,0 +1,329 @@ +// +// HMEmoticonTextView.m +// ������������ +// +// Created by ������ on 16/3/3. +// Copyright �� 2016��� itcast. All rights reserved. +// + +#import "HMEmoticonTextView.h" +#import "HMEmoticonAttachment.h" +#import "HMEmoticonInputView.h" + +@implementation HMEmoticonTextView { + UILabel *_placeHolderLabel; + UILabel *_lengthTipLabel; + NSMutableArray <NSLayoutConstraint *> *_lengthTipLabelCons; + + /// ������������������ + HMEmoticonInputView *_emoticonInputView; +} + +#pragma mark - ������������ +- (BOOL)isUseEmoticonInputView { + return self.inputView != nil; +} + +- (void)setUseEmoticonInputView:(BOOL)useEmoticonInputView { + + if (self.isUseEmoticonInputView == useEmoticonInputView) { + return; + } + + self.inputView = (self.inputView == nil) ? _emoticonInputView : nil; + [self reloadInputViews]; +} + +- (void)setPlaceholder:(NSString *)placeholder { + _placeholder = placeholder.copy; + _placeHolderLabel.text = _placeholder; +} + +- (void)setMaxInputLength:(NSInteger)maxInputLength { + _maxInputLength = maxInputLength; + + [self textChanged]; +} + +- (void)setFont:(UIFont *)font { + _placeHolderLabel.font = font; + + [super setFont:font]; +} + +- (void)setText:(NSString *)text { + [super setText:text]; + + [self textChanged]; +} + +- (UIColor *)textColor { + UIColor *color = [super textColor]; + + return (color == nil) ? [UIColor darkGrayColor] : color; +} + +- (void)setAttributedText:(NSAttributedString *)attributedText { + [super setAttributedText:attributedText]; + + [self textChanged]; +} + +#pragma mark - ������������ +- (instancetype)initWithFrame:(CGRect)frame textContainer:(NSTextContainer *)textContainer { + self = [super initWithFrame:frame textContainer:textContainer]; + + if (self) { + self.textColor = [UIColor darkGrayColor]; + self.font = [UIFont systemFontOfSize:18]; + + [self prepareUI]; + } + + return self; +} + +- (instancetype)initWithCoder:(NSCoder *)coder { + self = [super initWithCoder:coder]; + + if (self) { + [self prepareUI]; + } + + return self; +} + +- (void)willMoveToWindow:(UIWindow *)newWindow { + [super willMoveToWindow:newWindow]; + + [self layoutIfNeeded]; +} + +- (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; +} + +#pragma mark - ������������ +- (NSString *)emoticonText { + + NSAttributedString *attributeText = self.attributedText; + NSMutableString *stringM = [NSMutableString string]; + + [attributeText + enumerateAttributesInRange: + NSMakeRange(0, attributeText.length) + options:0 + usingBlock:^(NSDictionary<NSString *,id> * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) { + + HMEmoticonAttachment *attachment = attrs[@"NSAttachment"]; + if (attachment != nil) { + [stringM appendString:attachment.text]; + } else { + [stringM appendString:[attributeText.string substringWithRange:range]]; + } + }]; + + return stringM.copy; +} + +- (void)insertEmoticon:(HMEmoticon *)emoticon isRemoved:(BOOL)isRemoved { + + if (isRemoved) { + [self deleteBackward]; + + return; + } + + if (emoticon.isEmoji) { + [self replaceRange:[self selectedTextRange] withText:emoticon.emoji]; + + return; + } + + NSMutableAttributedString *attributeText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText]; + + NSAttributedString *emoticonStr = [HMEmoticonAttachment emoticonStringWithEmoticon:emoticon font:self.font textColor:self.textColor]; + + NSRange range = self.selectedRange; + [attributeText replaceCharactersInRange:range withAttributedString:emoticonStr]; + + self.attributedText = attributeText; + self.selectedRange = NSMakeRange(range.location + 1, 0); + + [self.delegate textViewDidChange:self]; + [[NSNotificationCenter defaultCenter] postNotificationName:UITextViewTextDidChangeNotification object:self]; +} + +- (void)updateTipLabelBottomConstraints:(UIView *)view { + + // ������ view ��������������������������� + if (![self.subviews containsObject:view]) { + NSLog(@"��������������������� textView ������������������������"); + return; + } + + [self removeConstraints:_lengthTipLabelCons]; + + [_lengthTipLabelCons removeAllObjects]; + CGFloat margin = 5; + [_lengthTipLabelCons addObject:[NSLayoutConstraint + constraintWithItem:_lengthTipLabel + attribute:NSLayoutAttributeTrailing + relatedBy:NSLayoutRelationEqual + toItem:self + attribute:NSLayoutAttributeTrailing + multiplier:1.0 + constant:-margin]]; + [_lengthTipLabelCons addObject:[NSLayoutConstraint + constraintWithItem:_lengthTipLabel + attribute:NSLayoutAttributeBottom + relatedBy:NSLayoutRelationEqual + toItem:view + attribute:NSLayoutAttributeTop + multiplier:1.0 + constant:-margin]]; + + [self addConstraints:_lengthTipLabelCons]; +} + +#pragma mark - ������������ +- (void)textChanged { + _placeHolderLabel.hidden = self.hasText; + + if (_maxInputLength <= 0) { + _lengthTipLabel.hidden = YES; + + return; + } + _lengthTipLabel.hidden = NO; + + NSInteger len = _maxInputLength - self.emoticonText.length; + _lengthTipLabel.text = [NSString stringWithFormat:@"%zd", len]; + _lengthTipLabel.textColor = (len >= 0) ? [UIColor lightGrayColor] : [UIColor redColor]; +} + +#pragma mark - ������������ +- (void)prepareInputView { + __weak typeof(self) weakSelf = self; + _emoticonInputView = [[HMEmoticonInputView alloc] initWithSelectedEmoticon:^(HMEmoticon * _Nullable emoticon, BOOL isRemoved) { + [weakSelf insertEmoticon:emoticon isRemoved:isRemoved]; + }]; +} + +- (void)prepareUI { + // 0. ������������������ + [self prepareInputView]; + + // 1. ������������������������ + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(textChanged) + name:UITextViewTextDidChangeNotification object:nil]; + + // 2. ������������ + self.alwaysBounceVertical = YES; + self.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; + + // 3. ������������ + UIView *sizeView = [[UIView alloc] init]; + sizeView.backgroundColor = [UIColor clearColor]; + sizeView.userInteractionEnabled = NO; + + [self insertSubview:sizeView atIndex:0]; + + sizeView.translatesAutoresizingMaskIntoConstraints = NO; + [self addConstraints:[NSLayoutConstraint + constraintsWithVisualFormat:@"H:|-0-[sizeView]-0-|" + options:0 + metrics:nil + views:@{@"sizeView": sizeView}]]; + [self addConstraints:[NSLayoutConstraint + constraintsWithVisualFormat:@"V:|-0-[sizeView]-0-|" + options:0 + metrics:nil + views:@{@"sizeView": sizeView}]]; + [self addConstraint:[NSLayoutConstraint + constraintWithItem:sizeView + attribute:NSLayoutAttributeWidth + relatedBy:NSLayoutRelationEqual + toItem:self + attribute:NSLayoutAttributeWidth + multiplier:1.0 + constant:0]]; + [self addConstraint:[NSLayoutConstraint + constraintWithItem:sizeView + attribute:NSLayoutAttributeHeight + relatedBy:NSLayoutRelationEqual + toItem:self + attribute:NSLayoutAttributeHeight + multiplier:1.0 + constant:0]]; + + // 4. ������������ + _placeHolderLabel = [[UILabel alloc] init]; + _placeHolderLabel.text = _placeholder; + _placeHolderLabel.textColor = [UIColor lightGrayColor]; + _placeHolderLabel.font = self.font; + _placeHolderLabel.numberOfLines = 0; + + [self addSubview:_placeHolderLabel]; + + _placeHolderLabel.translatesAutoresizingMaskIntoConstraints = NO; + CGFloat leftOffset = 5; + CGFloat topOffset = 8; + [self addConstraint:[NSLayoutConstraint + constraintWithItem:_placeHolderLabel + attribute:NSLayoutAttributeLeading + relatedBy:NSLayoutRelationEqual + toItem:self + attribute:NSLayoutAttributeLeading + multiplier:1.0 + constant:leftOffset]]; + [self addConstraint:[NSLayoutConstraint + constraintWithItem:_placeHolderLabel + attribute:NSLayoutAttributeTop + relatedBy:NSLayoutRelationEqual + toItem:self + attribute:NSLayoutAttributeTop + multiplier:1.0 + constant:topOffset]]; + [self addConstraint:[NSLayoutConstraint + constraintWithItem:_placeHolderLabel + attribute:NSLayoutAttributeWidth + relatedBy:NSLayoutRelationLessThanOrEqual + toItem:self + attribute:NSLayoutAttributeWidth + multiplier:1.0 + constant:-2 * leftOffset]]; + + // 5. ������������������ + _lengthTipLabel = [[UILabel alloc] init]; + _lengthTipLabel.text = [NSString stringWithFormat:@"%zd", _maxInputLength]; + _lengthTipLabel.textColor = [UIColor lightGrayColor]; + _lengthTipLabel.font = [UIFont systemFontOfSize:12]; + + [self addSubview:_lengthTipLabel]; + + _lengthTipLabel.translatesAutoresizingMaskIntoConstraints = NO; + _lengthTipLabelCons = [NSMutableArray array]; + [_lengthTipLabelCons addObject:[NSLayoutConstraint + constraintWithItem:_lengthTipLabel + attribute:NSLayoutAttributeTrailing + relatedBy:NSLayoutRelationEqual + toItem:self + attribute:NSLayoutAttributeTrailing + multiplier:1.0 + constant:-leftOffset]]; + [_lengthTipLabelCons addObject:[NSLayoutConstraint + constraintWithItem:_lengthTipLabel + attribute:NSLayoutAttributeBottom + relatedBy:NSLayoutRelationEqual + toItem:self + attribute:NSLayoutAttributeBottom + multiplier:1.0 + constant:-leftOffset]]; + + [self addConstraints:_lengthTipLabelCons]; +} + +@end -- Gitblit v1.8.0