UITextView 文字限制(可限制表情)

#import 
/**
 *  带默认提示的textView
 */


@class PlaceholderTextView;

@interface PlaceholderTextView : UITextView

@property (copy, nonatomic) NSString *placeholder;
@property (assign, nonatomic) NSInteger maxLength;//最大长度
@property (strong, nonatomic) UILabel *placeholderLabel;
@property (strong, nonatomic) UILabel *wordNumLabel;

//文字输入
@property (copy, nonatomic) void(^didChangeText)(PlaceholderTextView *textView);

- (void)didChangeText:(void(^)(PlaceholderTextView *textView))block;
- (void)updateCounter;

@end
#import "PlaceholderTextView.h"
#import "UPOCUtil.h"
@interface PlaceholderTextView ()

@property (strong, nonatomic) NSString *currentText;

@end


@implementation PlaceholderTextView

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addObserver];
        [self setView];
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self addObserver];
        [self setView];
    }
    return self;
}

-(id)init{
    self = [super init];
    if (self) {
        [self addObserver];
        [self setView];
    }
    return self;
}

-(void)setView{
    if (!self.placeholderLabel) {
        self.placeholderLabel = [[UILabel alloc]initWithFrame:CGRectMake(8, 0, self.frame.size.width, self.frame.size.height)];
        self.placeholderLabel.textColor = [UIColor lightGrayColor];
        self.placeholderLabel.numberOfLines = 0;
        self.placeholderLabel.font = [self font];
        [self addSubview:self.placeholderLabel];
        super.delegate = self;
    }
    
    if (!self.wordNumLabel) {
        self.wordNumLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        self.wordNumLabel.font = [UIFont systemFontOfSize:13];
        self.wordNumLabel.textColor = [UIColor lightGrayColor];
        self.wordNumLabel.textAlignment = NSTextAlignmentRight;
        [self addSubview:self.wordNumLabel];
    }
}

-(void)layoutSubviews{
    self.placeholderLabel.frame = CGRectMake(8, 6.5, self.frame.size.width-8, self.frame.size.height);
    [self.placeholderLabel sizeToFit];
    [self.wordNumLabel sizeToFit];
    [self refreshFram];
}

-(void)addObserver
{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidChangeNotification object:self];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(placeholderTextViewdidChange:) name:UITextViewTextDidChangeNotification object:self];
}



-(void)setPlaceholder:(NSString *)placeholder{
    _placeholder = placeholder;
    self.placeholderLabel.text = _placeholder;
    [self.placeholderLabel sizeToFit];
    [self endEditing:NO];
}
-(void)setMaxLength:(NSInteger)maxLength{
    
    _maxLength = maxLength;
    self.wordNumLabel.text = [NSString stringWithFormat:@"0/%ld",(long)_maxLength];
    
}
-(void)placeholderTextViewdidChange:(NSNotification *)notificat{
    PlaceholderTextView *textView = (PlaceholderTextView *)notificat.object;
    if([self.currentText rangeOfString:@"\n"].location != NSNotFound){
        self.text = [self.currentText stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        self.currentText = self.text;
        [textView resignFirstResponder];
    }
    if ([self.text length]>0) {
        [self.placeholderLabel setHidden:YES];
        [self.wordNumLabel setHidden:NO];
    }else{
        [self.placeholderLabel setHidden:NO];
        [self.wordNumLabel setHidden:NO];
    }
  
    
    if (self.maxLength!=0&&textView.markedTextRange == nil) {
        
        NSDictionary * result =  [UPOCUtil clipEmojiString:textView.text  ByLimit:self.maxLength];
        textView.text = result[@"text"];
        self.wordNumLabel.text = [NSString stringWithFormat:@"%@/%ld",result[@"length"],(long)_maxLength];
    }
  
    if (self.didChangeText) {
        self.didChangeText(textView);
    }
    
    [self refreshFram];
    _currentText = textView.text;
}


//- (NSDictionary*)clipEmojiString:(NSString *)string ByLimit:(NSUInteger)limit {
//    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"options:NSRegularExpressionCaseInsensitive error:nil];
//    NSArray *array = [regex matchesInString:string options:0 range:NSMakeRange(0, string.length)];
//     NSInteger count = 1;
//    if (array.count) {
//        NSRange range;
//        for (NSUInteger index = 0; index < string.length; index += range.length) {
//            range = [string rangeOfComposedCharacterSequenceAtIndex:index];
//            if (count==limit) {
//                 string = [string substringToIndex:range.location+range.length];
//                   break;
//            }
//            count++;
//        }
//    }else{
//          count = string.length;
//        if (string.length > limit) {
//             count = limit;
//            string = [string substringToIndex:limit];
//        }
//    }
//    return @{@"text":string,@"length":[NSString stringWithFormat:@"%ld",count]};
//}
- (void)updateCounter {
    self.wordNumLabel.text = [NSString stringWithFormat:@"%ld/%ld",(long)[self.text length],(long)_maxLength];
}

- (void)didChangeText:(void (^)(PlaceholderTextView *))block{
    self.didChangeText = block;
}


- (void)setText:(NSString *)text{
    [super setText:text];
    if (text.length>0) {
        [self.placeholderLabel setHidden:YES];
        self.wordNumLabel.text = [NSString stringWithFormat:@"%ld/%ld",(long)[text length],(long)_maxLength];
        [self.wordNumLabel sizeToFit];
        [self refreshFram];
    }
}

-(void)placeholderTextViewEndEditing{
    
    if ([self.text length]>0) {
        [self.placeholderLabel setHidden:YES];
        [self.wordNumLabel setHidden:NO];
    }else{
        [self.placeholderLabel setHidden:NO];
        [self.wordNumLabel setHidden:NO];
    }
}

- (void)refreshFram{
    [self.wordNumLabel sizeToFit];
    if (self.contentSize.height>self.frame.size.height-self.wordNumLabel.frame.size.height-5) {
        self.wordNumLabel.frame = CGRectMake(self.frame.size.width - self.wordNumLabel.frame.size.width-5, self.contentSize.height+self.contentInset.bottom-self.wordNumLabel.frame.size.height-5, self.wordNumLabel.frame.size.width, self.wordNumLabel.frame.size.height);
        self.contentInset = UIEdgeInsetsMake(0, 0, self.wordNumLabel.frame.size.height+5.0, 0);
    }else{
        self.wordNumLabel.frame = CGRectMake(self.frame.size.width - self.wordNumLabel.frame.size.width-5, self.frame.size.height + self.contentInset.bottom-self.wordNumLabel.frame.size.height-5, self.wordNumLabel.frame.size.width, self.wordNumLabel.frame.size.height);
        self.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
    if ([self.text length]>0) {
         [self.placeholderLabel setHidden:YES];
         [self.wordNumLabel setHidden:NO];
     }else{
         [self.placeholderLabel setHidden:NO];
         [self.wordNumLabel setHidden:NO];
     }
}
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    if (menuController) {
        [UIMenuController sharedMenuController].menuVisible = NO;
    }
    return NO;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
          
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end

你可能感兴趣的:(UITextView 文字限制(可限制表情))