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