处理点击一个label,再次点击一个label崩溃了
This commit is contained in:
@@ -16,6 +16,8 @@
|
|||||||
@property (nonatomic, strong) UIScrollView *scrollView;
|
@property (nonatomic, strong) UIScrollView *scrollView;
|
||||||
// 已创建的标签集合(顺序即显示顺序)
|
// 已创建的标签集合(顺序即显示顺序)
|
||||||
@property (nonatomic, strong) NSMutableArray<UILabel *> *labels;
|
@property (nonatomic, strong) NSMutableArray<UILabel *> *labels;
|
||||||
|
// 与 labels 对应的分割线
|
||||||
|
@property (nonatomic, strong) NSMutableArray<UIView *> *separatorLines;
|
||||||
// 每一段对应的“原始文本”(不含前缀编号),与 labels 一一对应
|
// 每一段对应的“原始文本”(不含前缀编号),与 labels 一一对应
|
||||||
@property (nonatomic, strong) NSMutableArray<NSString *> *segmentTexts;
|
@property (nonatomic, strong) NSMutableArray<NSString *> *segmentTexts;
|
||||||
// 文本缓冲:保存尚未遇到分隔符的尾部文本
|
// 文本缓冲:保存尚未遇到分隔符的尾部文本
|
||||||
@@ -23,6 +25,10 @@
|
|||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
static const CGFloat kKBStreamSeparatorHeight = 0.5f;
|
||||||
|
static const CGFloat kKBStreamSeparatorSpacingAbove = 5.0f;
|
||||||
|
static const CGFloat kKBStreamSeparatorSpacingBelow = 10.0f;
|
||||||
|
|
||||||
@implementation KBStreamTextView
|
@implementation KBStreamTextView
|
||||||
|
|
||||||
- (instancetype)initWithFrame:(CGRect)frame {
|
- (instancetype)initWithFrame:(CGRect)frame {
|
||||||
@@ -47,6 +53,7 @@
|
|||||||
_contentHorizontalPadding = 12.0;
|
_contentHorizontalPadding = 12.0;
|
||||||
_interItemSpacing = 5.0; // 标签之间的垂直间距 5pt
|
_interItemSpacing = 5.0; // 标签之间的垂直间距 5pt
|
||||||
_labels = [NSMutableArray array];
|
_labels = [NSMutableArray array];
|
||||||
|
_separatorLines = [NSMutableArray array];
|
||||||
_segmentTexts = [NSMutableArray array];
|
_segmentTexts = [NSMutableArray array];
|
||||||
_buffer = @"";
|
_buffer = @"";
|
||||||
_shouldTrimSegments = YES;
|
_shouldTrimSegments = YES;
|
||||||
@@ -147,6 +154,10 @@ static inline NSString *KBTrimRight(NSString *s) {
|
|||||||
[lbl removeFromSuperview];
|
[lbl removeFromSuperview];
|
||||||
}
|
}
|
||||||
[self.labels removeAllObjects];
|
[self.labels removeAllObjects];
|
||||||
|
for (UIView *line in self.separatorLines) {
|
||||||
|
[line removeFromSuperview];
|
||||||
|
}
|
||||||
|
[self.separatorLines removeAllObjects];
|
||||||
[self.segmentTexts removeAllObjects];
|
[self.segmentTexts removeAllObjects];
|
||||||
self.buffer = @"";
|
self.buffer = @"";
|
||||||
self.scrollView.contentSize = CGSizeMake(self.bounds.size.width, 0);
|
self.scrollView.contentSize = CGSizeMake(self.bounds.size.width, 0);
|
||||||
@@ -170,8 +181,13 @@ static inline NSString *KBTrimRight(NSString *s) {
|
|||||||
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleLabelTap:)];
|
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleLabelTap:)];
|
||||||
[label addGestureRecognizer:tap];
|
[label addGestureRecognizer:tap];
|
||||||
|
|
||||||
|
UIView *separator = [self buildSeparatorLine];
|
||||||
|
[self.scrollView addSubview:separator];
|
||||||
|
[self.separatorLines addObject:separator];
|
||||||
|
|
||||||
[self.scrollView addSubview:label];
|
[self.scrollView addSubview:label];
|
||||||
[self.labels addObject:label];
|
[self.labels addObject:label];
|
||||||
|
|
||||||
// 维护原始文本数组,与 labels 一一对应
|
// 维护原始文本数组,与 labels 一一对应
|
||||||
if (self.segmentTexts.count <= idx) {
|
if (self.segmentTexts.count <= idx) {
|
||||||
while (self.segmentTexts.count < idx) { [self.segmentTexts addObject:@""]; }
|
while (self.segmentTexts.count < idx) { [self.segmentTexts addObject:@""]; }
|
||||||
@@ -208,6 +224,10 @@ static inline NSString *KBTrimRight(NSString *s) {
|
|||||||
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleLabelTap:)];
|
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleLabelTap:)];
|
||||||
[label addGestureRecognizer:tap];
|
[label addGestureRecognizer:tap];
|
||||||
|
|
||||||
|
UIView *separator = [self buildSeparatorLine];
|
||||||
|
[self.scrollView addSubview:separator];
|
||||||
|
[self.separatorLines addObject:separator];
|
||||||
|
|
||||||
[self.scrollView addSubview:label];
|
[self.scrollView addSubview:label];
|
||||||
[self.labels addObject:label];
|
[self.labels addObject:label];
|
||||||
// 保证 segmentTexts 与 labels 对齐
|
// 保证 segmentTexts 与 labels 对齐
|
||||||
@@ -264,13 +284,22 @@ static inline NSString *KBTrimRight(NSString *s) {
|
|||||||
CGSize size = [self sizeForText:label.text font:label.font maxWidth:maxLabelWidth];
|
CGSize size = [self sizeForText:label.text font:label.font maxWidth:maxLabelWidth];
|
||||||
label.frame = CGRectMake(x, y, maxLabelWidth, size.height);
|
label.frame = CGRectMake(x, y, maxLabelWidth, size.height);
|
||||||
y += size.height;
|
y += size.height;
|
||||||
// 标签间距:下一个标签位于上一个标签下方 5pt
|
|
||||||
|
UIView *separator = (idx < self.separatorLines.count) ? self.separatorLines[idx] : nil;
|
||||||
|
CGFloat separatorTop = y + kKBStreamSeparatorSpacingAbove;
|
||||||
|
if (separator) {
|
||||||
|
separator.hidden = NO;
|
||||||
|
separator.frame = CGRectMake(x, separatorTop, maxLabelWidth, kKBStreamSeparatorHeight);
|
||||||
|
}
|
||||||
|
y = separatorTop + kKBStreamSeparatorHeight;
|
||||||
|
|
||||||
if (idx + 1 < self.labels.count) {
|
if (idx + 1 < self.labels.count) {
|
||||||
y += self.interItemSpacing;
|
y += kKBStreamSeparatorSpacingBelow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CGFloat contentHeight = MAX(y + self.interItemSpacing, self.bounds.size.height + 1.0);
|
y += self.interItemSpacing;
|
||||||
|
CGFloat contentHeight = MAX(y, self.bounds.size.height + 1.0);
|
||||||
self.scrollView.contentSize = CGSizeMake(width, contentHeight);
|
self.scrollView.contentSize = CGSizeMake(width, contentHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -316,11 +345,17 @@ static inline NSString *KBTrimRight(NSString *s) {
|
|||||||
UIInputViewController *ivc = KBFindInputViewController(self);
|
UIInputViewController *ivc = KBFindInputViewController(self);
|
||||||
if (ivc) {
|
if (ivc) {
|
||||||
id<UITextDocumentProxy> proxy = ivc.textDocumentProxy;
|
id<UITextDocumentProxy> proxy = ivc.textDocumentProxy;
|
||||||
if (rawText.length > 0 && [proxy conformsToProtocol:@protocol(UITextDocumentProxy)]) {
|
if ([proxy conformsToProtocol:@protocol(UITextDocumentProxy)]) {
|
||||||
|
NSString *context = proxy.documentContextBeforeInput ?: @"";
|
||||||
|
for (NSUInteger i = 0; i < context.length; i++) {
|
||||||
|
[proxy deleteBackward];
|
||||||
|
}
|
||||||
|
if (rawText.length > 0) {
|
||||||
[proxy insertText:rawText];
|
[proxy insertText:rawText];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#pragma mark - Segment Helpers
|
#pragma mark - Segment Helpers
|
||||||
|
|
||||||
@@ -358,4 +393,11 @@ static inline NSString *KBTrimRight(NSString *s) {
|
|||||||
label.text = [self displayTextForSegmentIndex:idx content:body];
|
label.text = [self displayTextForSegmentIndex:idx content:body];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (UIView *)buildSeparatorLine {
|
||||||
|
UIView *line = [[UIView alloc] initWithFrame:CGRectZero];
|
||||||
|
line.backgroundColor = [UIColor colorWithHex:0x02BEAC];
|
||||||
|
line.hidden = YES;
|
||||||
|
return line;
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
Reference in New Issue
Block a user