YYTextBindingExample.m 3.9 KB

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