YYTextTagExample.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // YYTextTagExample.m
  3. // YYKitExample
  4. //
  5. // Created by ibireme on 15/8/19.
  6. // Copyright (c) 2015 ibireme. All rights reserved.
  7. //
  8. #import "YYTextTagExample.h"
  9. #import "YYText.h"
  10. #import "UIView+YYAdd.h"
  11. #import "YYTextExampleHelper.h"
  12. @interface YYTextTagExample () <YYTextViewDelegate>
  13. @property (nonatomic, assign) YYTextView *textView;
  14. @end
  15. @implementation YYTextTagExample
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18. self.view.backgroundColor = [UIColor whiteColor];
  19. if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
  20. self.automaticallyAdjustsScrollViewInsets = NO;
  21. }
  22. NSMutableAttributedString *text = [NSMutableAttributedString new];
  23. NSArray *tags = @[@"◉red", @"◉orange", @"◉yellow", @"◉green", @"◉blue", @"◉purple", @"◉gray"];
  24. NSArray *tagStrokeColors = @[
  25. UIColorHex(fa3f39),
  26. UIColorHex(f48f25),
  27. UIColorHex(f1c02c),
  28. UIColorHex(54bc2e),
  29. UIColorHex(29a9ee),
  30. UIColorHex(c171d8),
  31. UIColorHex(818e91)
  32. ];
  33. NSArray *tagFillColors = @[
  34. UIColorHex(fb6560),
  35. UIColorHex(f6a550),
  36. UIColorHex(f3cc56),
  37. UIColorHex(76c957),
  38. UIColorHex(53baf1),
  39. UIColorHex(cd8ddf),
  40. UIColorHex(a4a4a7)
  41. ];
  42. UIFont *font = [UIFont boldSystemFontOfSize:16];
  43. for (int i = 0; i < tags.count; i++) {
  44. NSString *tag = tags[i];
  45. UIColor *tagStrokeColor = tagStrokeColors[i];
  46. UIColor *tagFillColor = tagFillColors[i];
  47. NSMutableAttributedString *tagText = [[NSMutableAttributedString alloc] initWithString:tag];
  48. [tagText yy_insertString:@"   " atIndex:0];
  49. [tagText yy_appendString:@"   "];
  50. tagText.yy_font = font;
  51. tagText.yy_color = [UIColor whiteColor];
  52. [tagText yy_setTextBinding:[YYTextBinding bindingWithDeleteConfirm:NO] range:tagText.yy_rangeOfAll];
  53. YYTextBorder *border = [YYTextBorder new];
  54. border.strokeWidth = 1.5;
  55. border.strokeColor = tagStrokeColor;
  56. border.fillColor = tagFillColor;
  57. border.cornerRadius = 100; // a huge value
  58. border.lineJoin = kCGLineJoinBevel;
  59. border.insets = UIEdgeInsetsMake(-2, -5.5, -2, -8);
  60. [tagText yy_setTextBackgroundBorder:border range:[tagText.string rangeOfString:tag]];
  61. [text appendAttributedString:tagText];
  62. }
  63. text.yy_lineSpacing = 10;
  64. text.yy_lineBreakMode = NSLineBreakByWordWrapping;
  65. [text yy_appendString:@"\n"];
  66. [text appendAttributedString:text]; // repeat for test
  67. YYTextView *textView = [YYTextView new];
  68. textView.attributedText = text;
  69. textView.size = self.view.size;
  70. textView.textContainerInset = UIEdgeInsetsMake(10 + 64, 10, 10, 10);
  71. textView.allowsCopyAttributedString = YES;
  72. textView.allowsPasteAttributedString = YES;
  73. textView.delegate = self;
  74. if (kiOS7Later) {
  75. textView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
  76. } else {
  77. textView.height -= 64;
  78. }
  79. textView.scrollIndicatorInsets = textView.contentInset;
  80. textView.selectedRange = NSMakeRange(text.length, 0);
  81. [self.view addSubview:textView];
  82. self.textView = textView;
  83. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  84. [textView becomeFirstResponder];
  85. });
  86. }
  87. - (void)edit:(UIBarButtonItem *)item {
  88. if (_textView.isFirstResponder) {
  89. [_textView resignFirstResponder];
  90. } else {
  91. [_textView becomeFirstResponder];
  92. }
  93. }
  94. #pragma mark text view
  95. - (void)textViewDidBeginEditing:(YYTextView *)textView {
  96. UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
  97. target:self
  98. action:@selector(edit:)];
  99. self.navigationItem.rightBarButtonItem = buttonItem;
  100. }
  101. - (void)textViewDidEndEditing:(YYTextView *)textView {
  102. self.navigationItem.rightBarButtonItem = nil;
  103. }
  104. @end