处理点击一个label,再次点击一个label崩溃了
This commit is contained in:
@@ -16,6 +16,8 @@
|
||||
@property (nonatomic, strong) UIScrollView *scrollView;
|
||||
// 已创建的标签集合(顺序即显示顺序)
|
||||
@property (nonatomic, strong) NSMutableArray<UILabel *> *labels;
|
||||
// 与 labels 对应的分割线
|
||||
@property (nonatomic, strong) NSMutableArray<UIView *> *separatorLines;
|
||||
// 每一段对应的“原始文本”(不含前缀编号),与 labels 一一对应
|
||||
@property (nonatomic, strong) NSMutableArray<NSString *> *segmentTexts;
|
||||
// 文本缓冲:保存尚未遇到分隔符的尾部文本
|
||||
@@ -23,6 +25,10 @@
|
||||
|
||||
@end
|
||||
|
||||
static const CGFloat kKBStreamSeparatorHeight = 0.5f;
|
||||
static const CGFloat kKBStreamSeparatorSpacingAbove = 5.0f;
|
||||
static const CGFloat kKBStreamSeparatorSpacingBelow = 10.0f;
|
||||
|
||||
@implementation KBStreamTextView
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame {
|
||||
@@ -47,6 +53,7 @@
|
||||
_contentHorizontalPadding = 12.0;
|
||||
_interItemSpacing = 5.0; // 标签之间的垂直间距 5pt
|
||||
_labels = [NSMutableArray array];
|
||||
_separatorLines = [NSMutableArray array];
|
||||
_segmentTexts = [NSMutableArray array];
|
||||
_buffer = @"";
|
||||
_shouldTrimSegments = YES;
|
||||
@@ -147,6 +154,10 @@ static inline NSString *KBTrimRight(NSString *s) {
|
||||
[lbl removeFromSuperview];
|
||||
}
|
||||
[self.labels removeAllObjects];
|
||||
for (UIView *line in self.separatorLines) {
|
||||
[line removeFromSuperview];
|
||||
}
|
||||
[self.separatorLines removeAllObjects];
|
||||
[self.segmentTexts removeAllObjects];
|
||||
self.buffer = @"";
|
||||
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:)];
|
||||
[label addGestureRecognizer:tap];
|
||||
|
||||
UIView *separator = [self buildSeparatorLine];
|
||||
[self.scrollView addSubview:separator];
|
||||
[self.separatorLines addObject:separator];
|
||||
|
||||
[self.scrollView addSubview:label];
|
||||
[self.labels addObject:label];
|
||||
|
||||
// 维护原始文本数组,与 labels 一一对应
|
||||
if (self.segmentTexts.count <= idx) {
|
||||
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:)];
|
||||
[label addGestureRecognizer:tap];
|
||||
|
||||
UIView *separator = [self buildSeparatorLine];
|
||||
[self.scrollView addSubview:separator];
|
||||
[self.separatorLines addObject:separator];
|
||||
|
||||
[self.scrollView addSubview:label];
|
||||
[self.labels addObject:label];
|
||||
// 保证 segmentTexts 与 labels 对齐
|
||||
@@ -264,13 +284,22 @@ static inline NSString *KBTrimRight(NSString *s) {
|
||||
CGSize size = [self sizeForText:label.text font:label.font maxWidth:maxLabelWidth];
|
||||
label.frame = CGRectMake(x, y, maxLabelWidth, 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) {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -316,8 +345,14 @@ static inline NSString *KBTrimRight(NSString *s) {
|
||||
UIInputViewController *ivc = KBFindInputViewController(self);
|
||||
if (ivc) {
|
||||
id<UITextDocumentProxy> proxy = ivc.textDocumentProxy;
|
||||
if (rawText.length > 0 && [proxy conformsToProtocol:@protocol(UITextDocumentProxy)]) {
|
||||
[proxy insertText:rawText];
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -358,4 +393,11 @@ static inline NSString *KBTrimRight(NSString *s) {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user