处理点击一个label,再次点击一个label崩溃了

This commit is contained in:
2025-12-15 14:21:42 +08:00
parent 64e0218add
commit 1f07120289

View File

@@ -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