测试假数据

This commit is contained in:
2025-11-11 21:48:26 +08:00
parent 1d064c1f31
commit f387b95d0d
2 changed files with 76 additions and 3 deletions

View File

@@ -34,6 +34,12 @@ static NSString * const kKBFunctionTagCellId = @"KBFunctionTagCellId";
//
@property (nonatomic, strong, nullable) KBStreamTextView *streamTextView;
@property (nonatomic, strong, nullable) UIButton *streamDeleteButton;
// &
@property (nonatomic, strong, nullable) NSTimer *streamMockTimer;
@property (nonatomic, strong, nullable) NSArray<NSString *> *streamMockChunks;
@property (nonatomic, assign) NSInteger streamMockIndex;
@property (nonatomic, copy, nullable) NSString *streamMockSource; // \t
@property (nonatomic, assign) NSInteger streamMockCursor; //
// Data
@property (nonatomic, strong) NSArray<NSString *> *itemsInternal;
@@ -73,6 +79,7 @@ static NSString * const kKBFunctionTagCellId = @"KBFunctionTagCellId";
- (void)dealloc {
[self stopPasteboardMonitor];
[self kb_stopMockStreaming];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@@ -204,7 +211,7 @@ static NSString * const kKBFunctionTagCellId = @"KBFunctionTagCellId";
[self addSubview:sv];
[sv mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.mas_left).offset(12);
make.right.equalTo(self).offset(-12);
make.right.equalTo(self.rightButtonContainer.mas_left).offset(-12);
make.top.equalTo(self.pasteViewInternal.mas_bottom).offset(10);
make.bottom.equalTo(self.mas_bottom).offset(-10);
}];
@@ -226,10 +233,14 @@ static NSString * const kKBFunctionTagCellId = @"KBFunctionTagCellId";
make.width.mas_greaterThanOrEqualTo(56);
}];
self.streamDeleteButton = del;
// KBStreamTextView
[self kb_startMockStreamingWithSeed:title];
}
- (void)kb_onTapStreamDelete {
//
[self kb_stopMockStreaming];
[self.streamDeleteButton removeFromSuperview];
self.streamDeleteButton = nil;
[self.streamTextView removeFromSuperview];
@@ -237,6 +248,56 @@ static NSString * const kKBFunctionTagCellId = @"KBFunctionTagCellId";
self.collectionViewInternal.hidden = NO;
}
#pragma mark - Mock Streaming
// KBStreamTextView
- (void)kb_startMockStreamingWithSeed:(NSString *)seedTitle {
[self kb_stopMockStreaming];
// \t \n 1~3
NSString *t = seedTitle ?: @"高情商";
self.streamMockSource = [NSString stringWithFormat:
@"选择了标签:%@\n"
"下面演示流式输出,每隔一小段时间来一截。第一段结束。\t"
"第二段开始:这是一个较长的句子,会被拆成多个小块推送,直到遇到分隔符就换段。\n第二段结束。\t"
"最后一段:右上角可随时关闭展示。\n演示完成。\t",
t];
self.streamMockCursor = 0;
self.streamMockChunks = nil; //
self.streamMockIndex = 0;
// 线 1~3
__weak typeof(self) weakSelf = self;
self.streamMockTimer = [NSTimer scheduledTimerWithTimeInterval:0.20 repeats:YES block:^(NSTimer * _Nonnull timer) {
__strong typeof(weakSelf) self = weakSelf; if (!self) { [timer invalidate]; return; }
if (!self.streamTextView) { [timer invalidate]; return; }
NSInteger total = (NSInteger)self.streamMockSource.length;
if (self.streamMockCursor >= total) {
//
[self.streamTextView finishStreaming];
[timer invalidate];
self.streamMockTimer = nil;
return;
}
// 1~3
uint32_t step = arc4random_uniform(3) + 1; // 1..3
NSInteger remain = total - self.streamMockCursor;
NSInteger take = MIN((NSInteger)step, remain);
NSString *piece = [self.streamMockSource substringWithRange:NSMakeRange(self.streamMockCursor, take)];
self.streamMockCursor += take;
[self.streamTextView appendStreamText:piece ?: @""];
}];
}
- (void)kb_stopMockStreaming {
[self.streamMockTimer invalidate];
self.streamMockTimer = nil;
self.streamMockChunks = nil;
self.streamMockIndex = 0;
self.streamMockSource = nil;
self.streamMockCursor = 0;
}
// UL App Scheme访
// 访 KBStreamTextView
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

View File

@@ -8,6 +8,7 @@
//
#import "KBStreamTextView.h"
#import "KBResponderUtils.h" // UIInputViewController宿
@interface KBStreamTextView ()
@@ -170,7 +171,7 @@
label.textColor = self.labelTextColor;
label.userInteractionEnabled = YES;
label.text = @"";
label.backgroundColor = [UIColor redColor];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleLabelTap:)];
[label addGestureRecognizer:tap];
@@ -256,8 +257,19 @@
UILabel *label = (UILabel *)tap.view;
if (![label isKindOfClass:[UILabel class]]) { return; }
NSInteger index = [self.labels indexOfObject:label];
NSString *text = label.text ?: @"";
if (index != NSNotFound && self.onLabelTap) {
self.onLabelTap(index, label.text ?: @"");
self.onLabelTap(index, text);
}
// 宿/TextView
// 访 insertText:
UIInputViewController *ivc = KBFindInputViewController(self);
if (ivc) {
id<UITextDocumentProxy> proxy = ivc.textDocumentProxy;
if (text.length > 0 && [proxy conformsToProtocol:@protocol(UITextDocumentProxy)]) {
[proxy insertText:text];
}
}
}