This commit is contained in:
2025-11-11 19:39:33 +08:00
parent 20b13bcffa
commit 3440cc4773
4 changed files with 349 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
//
// KBStreamTextView.h
// KeyBoard
//
// 一个可滚动的视图,用于接收“流式”文本输入。
// 当检测到分隔符(默认: "\t" 制表符)时,会将当前累计的文本作为一个段落
// 创建一个新的 UILabel每个标签支持自动换行和点击事件。
// 适用于流式数据逐步到达、按段落追加展示的场景。
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
typedef void (^KBStreamTextTapHandler)(NSInteger index, NSString *text);
@interface KBStreamTextView : UIView
/// 分段分隔符,默认 "\t"(制表符)。
@property (nonatomic, copy) NSString *delimiter;
/// 标签使用的字体,默认系统 16 号。
@property (nonatomic, strong) UIFont *labelFont;
/// 标签文本颜色iOS 13+ 默认 labelColor低版本默认黑色。
@property (nonatomic, strong) UIColor *labelTextColor;
/// 水平内边距(左右留白),默认 12。
@property (nonatomic, assign) CGFloat contentHorizontalPadding;
/// 标签间的垂直间距,默认 5。
@property (nonatomic, assign) CGFloat interItemSpacing;
/// 标签点击回调,提供被点击的序号与文本。
@property (nonatomic, copy, nullable) KBStreamTextTapHandler onLabelTap;
/// 是否裁剪各段落前后的空白/换行,默认 YES。
@property (nonatomic, assign) BOOL shouldTrimSegments;
/// 追加流式文本(边输边见):
/// - 实时将未完成段落展示在“当前标签”上;
/// - 当遇到分隔符时,先将当前标签视为“完成段”,可选裁剪空白,再创建一个新的空标签作为下一段的容器。
- (void)appendStreamText:(NSString *)text;
/// 清空所有标签并重置内部缓冲。
- (void)reset;
/// 结束输入:将当前正在输入的段落视为完成段(按需裁剪),但不会再新建标签。
- (void)finishStreaming;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,264 @@
//
// KBStreamTextView.m
// KeyBoard
//
//
// "\t" UILabel
// UIScrollView
//
#import "KBStreamTextView.h"
@interface KBStreamTextView ()
//
@property (nonatomic, strong) UIScrollView *scrollView;
//
@property (nonatomic, strong) NSMutableArray<UILabel *> *labels;
//
@property (nonatomic, copy) NSString *buffer;
@end
@implementation KBStreamTextView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self commonInit];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder {
if (self = [super initWithCoder:coder]) {
[self commonInit];
}
return self;
}
- (void)commonInit {
_delimiter = @"\t";
_labelFont = [UIFont systemFontOfSize:16.0];
if (@available(iOS 13.0, *)) {
_labelTextColor = [UIColor labelColor];
} else {
_labelTextColor = [UIColor blackColor];
}
_contentHorizontalPadding = 12.0;
_interItemSpacing = 5.0; // 5pt
_labels = [NSMutableArray array];
_buffer = @"";
_shouldTrimSegments = YES;
//
_scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
_scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_scrollView.alwaysBounceVertical = YES;
_scrollView.showsVerticalScrollIndicator = YES;
[self addSubview:_scrollView];
}
#pragma mark - Public API
// label label
- (void)appendStreamText:(NSString *)text {
if (text.length == 0) { return; }
// 线线 UI
if (![NSThread isMainThread]) {
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf appendStreamText:text];
});
return;
}
// 退
if (self.delimiter.length == 0) {
[self ensureCurrentLabelExists];
self.buffer = [self.buffer stringByAppendingString:text];
self.labels.lastObject.text = self.buffer;
[self layoutLabelsForCurrentWidth];
[self scrollToBottomIfNeeded];
return;
}
// label
[self ensureCurrentLabelExists];
// parts.count = = parts.count - 1
NSArray<NSString *> *parts = [text componentsSeparatedByString:self.delimiter];
// 1)
self.buffer = [self.buffer stringByAppendingString:parts.firstObject ?: @""];
self.labels.lastObject.text = self.buffer; //
[self layoutLabelsForCurrentWidth];
// 2)
for (NSUInteger i = 1; i < parts.count; i++) {
// a)
UILabel *current = self.labels.lastObject;
if (self.shouldTrimSegments) {
NSString *trimmed = [current.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
current.text = trimmed;
}
[self layoutLabelsForCurrentWidth];
// b)
[self createEmptyLabelAsNewSegment];
// c)
NSString *piece = parts[i];
self.buffer = piece ?: @"";
self.labels.lastObject.text = self.buffer;
[self layoutLabelsForCurrentWidth];
}
[self scrollToBottomIfNeeded];
}
- (void)reset {
for (UILabel *lbl in self.labels) {
[lbl removeFromSuperview];
}
[self.labels removeAllObjects];
self.buffer = @"";
self.scrollView.contentSize = CGSizeMake(self.bounds.size.width, 0);
}
#pragma mark - Layout Helpers
- (void)addLabelForText:(NSString *)text {
if (self.shouldTrimSegments) {
text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
// UILabel
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.numberOfLines = 0;
label.font = self.labelFont;
label.textColor = self.labelTextColor;
label.userInteractionEnabled = YES; //
label.text = text;
//
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleLabelTap:)];
[label addGestureRecognizer:tap];
[self.scrollView addSubview:label];
[self.labels addObject:label];
//
[self layoutLabelsForCurrentWidth];
//
[self scrollToBottomIfNeeded];
}
#pragma mark - Streaming Helpers
// label
- (void)ensureCurrentLabelExists {
if (self.labels.lastObject) { return; }
[self createEmptyLabelAsNewSegment];
}
//
- (void)createEmptyLabelAsNewSegment {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.numberOfLines = 0;
label.font = self.labelFont;
label.textColor = self.labelTextColor;
label.userInteractionEnabled = YES;
label.text = @"";
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleLabelTap:)];
[label addGestureRecognizer:tap];
[self.scrollView addSubview:label];
[self.labels addObject:label];
self.buffer = @"";
[self layoutLabelsForCurrentWidth];
}
- (void)finishStreaming {
if (![NSThread isMainThread]) {
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf finishStreaming];
});
return;
}
UILabel *current = self.labels.lastObject;
if (!current) { return; }
if (self.shouldTrimSegments) {
NSString *trimmed = [current.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
current.text = trimmed;
self.buffer = trimmed;
[self layoutLabelsForCurrentWidth];
}
}
- (void)layoutSubviews {
[super layoutSubviews];
// /
[self layoutLabelsForCurrentWidth];
}
- (void)layoutLabelsForCurrentWidth {
CGFloat width = self.bounds.size.width;
if (width <= 0) { return; }
CGFloat x = self.contentHorizontalPadding;
CGFloat maxLabelWidth = MAX(0.0, width - 2.0 * self.contentHorizontalPadding);
CGFloat y = self.interItemSpacing; //
for (NSUInteger idx = 0; idx < self.labels.count; idx++) {
UILabel *label = self.labels[idx];
CGSize size = [self sizeForText:label.text font:label.font maxWidth:maxLabelWidth];
label.frame = CGRectMake(x, y, maxLabelWidth, size.height);
y += size.height;
// 5pt
if (idx + 1 < self.labels.count) {
y += self.interItemSpacing;
}
}
CGFloat contentHeight = MAX(y + self.interItemSpacing, self.bounds.size.height + 1.0);
self.scrollView.contentSize = CGSizeMake(width, contentHeight);
}
- (CGSize)sizeForText:(NSString *)text font:(UIFont *)font maxWidth:(CGFloat)maxWidth {
if (text.length == 0) {
//
return CGSizeMake(maxWidth, font.lineHeight);
}
CGRect rect = [text boundingRectWithSize:CGSizeMake(maxWidth, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName: font}
context:nil];
//
return CGSizeMake(maxWidth, ceil(rect.size.height));
}
- (void)scrollToBottomIfNeeded {
CGFloat height = self.scrollView.bounds.size.height;
CGFloat contentHeight = self.scrollView.contentSize.height;
if (contentHeight > height && height > 0) {
CGPoint bottomOffset = CGPointMake(0, contentHeight - height);
[self.scrollView setContentOffset:bottomOffset animated:YES];
}
}
#pragma mark - Tap Handling
- (void)handleLabelTap:(UITapGestureRecognizer *)tap {
UILabel *label = (UILabel *)tap.view;
if (![label isKindOfClass:[UILabel class]]) { return; }
NSInteger index = [self.labels indexOfObject:label];
if (index != NSNotFound && self.onLabelTap) {
self.onLabelTap(index, label.text ?: @"");
}
}
@end

View File

@@ -86,6 +86,7 @@
049FB2262EC3136D00FAB05D /* KBPersonInfoItemCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 049FB2252EC3136D00FAB05D /* KBPersonInfoItemCell.m */; };
049FB2292EC31BB000FAB05D /* KBChangeNicknamePopView.m in Sources */ = {isa = PBXBuildFile; fileRef = 049FB2282EC31BB000FAB05D /* KBChangeNicknamePopView.m */; };
049FB22C2EC31F8800FAB05D /* KBGenderPickerPopView.m in Sources */ = {isa = PBXBuildFile; fileRef = 049FB22B2EC31F8800FAB05D /* KBGenderPickerPopView.m */; };
049FB22F2EC34EB900FAB05D /* KBStreamTextView.m in Sources */ = {isa = PBXBuildFile; fileRef = 049FB22E2EC34EB900FAB05D /* KBStreamTextView.m */; };
049FB31D2EC21BCD00FAB05D /* KBMyKeyboardCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 049FB31C2EC21BCD00FAB05D /* KBMyKeyboardCell.m */; };
04A9FE0F2EB481100020DB6D /* KBHUD.m in Sources */ = {isa = PBXBuildFile; fileRef = 04FC97082EB31B14007BD342 /* KBHUD.m */; };
04A9FE132EB4D0D20020DB6D /* KBFullAccessManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 04A9FE112EB4D0D20020DB6D /* KBFullAccessManager.m */; };
@@ -299,6 +300,8 @@
049FB2282EC31BB000FAB05D /* KBChangeNicknamePopView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KBChangeNicknamePopView.m; sourceTree = "<group>"; };
049FB22A2EC31F8800FAB05D /* KBGenderPickerPopView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KBGenderPickerPopView.h; sourceTree = "<group>"; };
049FB22B2EC31F8800FAB05D /* KBGenderPickerPopView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KBGenderPickerPopView.m; sourceTree = "<group>"; };
049FB22D2EC34EB900FAB05D /* KBStreamTextView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KBStreamTextView.h; sourceTree = "<group>"; };
049FB22E2EC34EB900FAB05D /* KBStreamTextView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KBStreamTextView.m; sourceTree = "<group>"; };
049FB31B2EC21BCD00FAB05D /* KBMyKeyboardCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KBMyKeyboardCell.h; sourceTree = "<group>"; };
049FB31C2EC21BCD00FAB05D /* KBMyKeyboardCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KBMyKeyboardCell.m; sourceTree = "<group>"; };
04A9A67D2EB9E1690023B8F4 /* KBResponderUtils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KBResponderUtils.h; sourceTree = "<group>"; };
@@ -748,6 +751,8 @@
A1B2C3F22EB35A9900000001 /* KBFullAccessGuideView.m */,
04FC95B02EB0B2CC007BD342 /* KBSettingView.h */,
04FC95B12EB0B2CC007BD342 /* KBSettingView.m */,
049FB22D2EC34EB900FAB05D /* KBStreamTextView.h */,
049FB22E2EC34EB900FAB05D /* KBStreamTextView.m */,
);
path = View;
sourceTree = "<group>";
@@ -1392,6 +1397,7 @@
A1B2C3E22EB0C0A100000001 /* KBNetworkManager.m in Sources */,
04FC956A2EB05497007BD342 /* KBKeyButton.m in Sources */,
04FC95B22EB0B2CC007BD342 /* KBSettingView.m in Sources */,
049FB22F2EC34EB900FAB05D /* KBStreamTextView.m in Sources */,
04FC95702EB09516007BD342 /* KBFunctionView.m in Sources */,
04FC956D2EB054B7007BD342 /* KBKeyboardView.m in Sources */,
04FC95672EB0546C007BD342 /* KBKey.m in Sources */,

View File

@@ -8,6 +8,7 @@
#import "HomeMainVC.h"
#import "HomeHeadView.h"
#import "KBPanModalView.h"
#import "KBGuideVC.h" //
@interface HomeMainVC ()
@property (nonatomic, strong) HomeHeadView *headView;
@@ -43,6 +44,31 @@
[self.simplePanModalView presentInView:self.view];
}
// viewDidLoad push
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self kb_showGuideIfFirstLaunch];
}
/// KBGuideVC
- (void)kb_showGuideIfFirstLaunch {
static NSString *const kKBHasLaunchedOnce = @"KBHasLaunchedOnce";
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
if (![ud boolForKey:kKBHasLaunchedOnce]) {
[ud setBool:YES forKey:kKBHasLaunchedOnce];
[ud synchronize];
// push线
if (self.navigationController && self.presentedViewController == nil) {
KBGuideVC *vc = [KBGuideVC new];
vc.hidesBottomBarWhenPushed = YES;
dispatch_async(dispatch_get_main_queue(), ^{
[self.navigationController pushViewController:vc animated:YES];
});
}
}
}
- (void)setupMas{
[self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);