123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- //
- // YYTextBindingExample.m
- // YYKitExample
- //
- // Created by ibireme on 15/9/3.
- // Copyright (c) 2015 ibireme. All rights reserved.
- //
- #import "YYTextBindingExample.h"
- #import "YYText.h"
- #import "YYImage.h"
- #import "UIImage+YYWebImage.h"
- #import "UIView+YYAdd.h"
- #import "NSBundle+YYAdd.h"
- #import "NSString+YYAdd.h"
- #import "YYTextExampleHelper.h"
- @interface YYTextExampleEmailBindingParser :NSObject <YYTextParser>
- @property (nonatomic, strong) NSRegularExpression *regex;
- @end
- @implementation YYTextExampleEmailBindingParser
- - (instancetype)init {
- self = [super init];
- NSString *pattern = @"[-_a-zA-Z@\\.]+[ ,\\n]";
- self.regex = [[NSRegularExpression alloc] initWithPattern:pattern options:kNilOptions error:nil];
- return self;
- }
- - (BOOL)parseText:(NSMutableAttributedString *)text selectedRange:(NSRangePointer)range {
- __block BOOL changed = NO;
- [_regex enumerateMatchesInString:text.string options:NSMatchingWithoutAnchoringBounds range:text.yy_rangeOfAll usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
- if (!result) return;
- NSRange range = result.range;
- if (range.location == NSNotFound || range.length < 1) return;
- if ([text attribute:YYTextBindingAttributeName atIndex:range.location effectiveRange:NULL]) return;
-
- NSRange bindlingRange = NSMakeRange(range.location, range.length - 1);
- YYTextBinding *binding = [YYTextBinding bindingWithDeleteConfirm:YES];
- [text yy_setTextBinding:binding range:bindlingRange]; /// Text binding
- [text yy_setColor:[UIColor colorWithRed:0.000 green:0.519 blue:1.000 alpha:1.000] range:bindlingRange];
- changed = YES;
- }];
- return changed;
- }
- @end
- @interface YYTextBindingExample () <YYTextViewDelegate>
- @property (nonatomic, strong) YYTextView *textView;
- @property (nonatomic, assign) BOOL isInEdit;
- @end
- @implementation YYTextBindingExample
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor whiteColor];
- if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
- self.automaticallyAdjustsScrollViewInsets = NO;
- }
-
- NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"sjobs@apple.com, apple@apple.com, banana@banana.com, pear@pear.com "];
- text.yy_font = [UIFont systemFontOfSize:17];
- text.yy_lineSpacing = 5;
- text.yy_color = [UIColor blackColor];
-
- YYTextView *textView = [YYTextView new];
- textView.attributedText = text;
- textView.textParser = [YYTextExampleEmailBindingParser new];
- textView.size = self.view.size;
- textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
- textView.delegate = self;
- if (kiOS7Later) {
- textView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
- }
- textView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
- textView.scrollIndicatorInsets = textView.contentInset;
- [self.view addSubview:textView];
- self.textView = textView;
- [self.textView becomeFirstResponder];
-
- }
- - (void)edit:(UIBarButtonItem *)item {
- if (_textView.isFirstResponder) {
- [_textView resignFirstResponder];
- } else {
- [_textView becomeFirstResponder];
- }
- }
- - (void)textViewDidChange:(YYTextView *)textView {
- if (textView.text.length == 0) {
- textView.textColor = [UIColor blackColor];
- }
- }
- - (void)textViewDidBeginEditing:(YYTextView *)textView {
- UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
- target:self
- action:@selector(edit:)];
- self.navigationItem.rightBarButtonItem = buttonItem;
- }
- - (void)textViewDidEndEditing:(YYTextView *)textView {
- self.navigationItem.rightBarButtonItem = nil;
- }
- @end
|