YYTextTagExample.m 4.0 KB

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