YYTextBindingExample.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // YYTextBindingExample.m
  3. // YYKitExample
  4. //
  5. // Created by ibireme on 15/9/3.
  6. // Copyright (c) 2015 ibireme. All rights reserved.
  7. //
  8. #import "YYTextBindingExample.h"
  9. #import "YYKit.h"
  10. @interface YYTextExampleEmailBindingParser :NSObject <YYTextParser>
  11. @property (nonatomic, strong) NSRegularExpression *regex;
  12. @end
  13. @implementation YYTextExampleEmailBindingParser
  14. - (instancetype)init {
  15. self = [super init];
  16. NSString *pattern = @"[-_a-zA-Z@\\.]+[ ,\\n]";
  17. self.regex = [[NSRegularExpression alloc] initWithPattern:pattern options:kNilOptions error:nil];
  18. return self;
  19. }
  20. - (BOOL)parseText:(NSMutableAttributedString *)text selectedRange:(NSRangePointer)range {
  21. __block BOOL changed = NO;
  22. [_regex enumerateMatchesInString:text.string options:NSMatchingWithoutAnchoringBounds range:text.rangeOfAll usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
  23. if (!result) return;
  24. NSRange range = result.range;
  25. if (range.location == NSNotFound || range.length < 1) return;
  26. if ([text attribute:YYTextBindingAttributeName atIndex:range.location effectiveRange:NULL]) return;
  27. NSRange bindlingRange = NSMakeRange(range.location, range.length - 1);
  28. YYTextBinding *binding = [YYTextBinding bindingWithDeleteConfirm:YES];
  29. [text setTextBinding:binding range:bindlingRange]; /// Text binding
  30. [text setColor:[UIColor colorWithRed:0.000 green:0.519 blue:1.000 alpha:1.000] range:bindlingRange];
  31. changed = YES;
  32. }];
  33. return changed;
  34. }
  35. @end
  36. @interface YYTextBindingExample () <YYTextViewDelegate>
  37. @property (nonatomic, strong) YYTextView *textView;
  38. @property (nonatomic, assign) BOOL isInEdit;
  39. @end
  40. @implementation YYTextBindingExample
  41. - (void)viewDidLoad {
  42. [super viewDidLoad];
  43. self.view.backgroundColor = [UIColor whiteColor];
  44. if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
  45. self.automaticallyAdjustsScrollViewInsets = NO;
  46. }
  47. NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"sjobs@apple.com, apple@apple.com, banana@banana.com, pear@pear.com "];
  48. text.font = [UIFont systemFontOfSize:17];
  49. text.lineSpacing = 5;
  50. text.color = [UIColor blackColor];
  51. YYTextView *textView = [YYTextView new];
  52. textView.attributedText = text;
  53. textView.textParser = [YYTextExampleEmailBindingParser new];
  54. textView.size = self.view.size;
  55. textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
  56. textView.delegate = self;
  57. if (kiOS7Later) {
  58. textView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
  59. }
  60. textView.contentInset = UIEdgeInsetsMake((kiOS7Later ? 64 : 0), 0, 0, 0);
  61. textView.scrollIndicatorInsets = textView.contentInset;
  62. [self.view addSubview:textView];
  63. self.textView = textView;
  64. [self.textView becomeFirstResponder];
  65. }
  66. - (void)edit:(UIBarButtonItem *)item {
  67. if (_textView.isFirstResponder) {
  68. [_textView resignFirstResponder];
  69. } else {
  70. [_textView becomeFirstResponder];
  71. }
  72. }
  73. - (void)textViewDidChange:(YYTextView *)textView {
  74. if (textView.text.length == 0) {
  75. textView.textColor = [UIColor blackColor];
  76. }
  77. }
  78. - (void)textViewDidBeginEditing:(YYTextView *)textView {
  79. UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
  80. target:self
  81. action:@selector(edit:)];
  82. self.navigationItem.rightBarButtonItem = buttonItem;
  83. }
  84. - (void)textViewDidEndEditing:(YYTextView *)textView {
  85. self.navigationItem.rightBarButtonItem = nil;
  86. }
  87. @end