2
This commit is contained in:
@@ -62,6 +62,20 @@
|
||||
[self addSubview:_scrollView];
|
||||
}
|
||||
|
||||
#pragma mark - Helpers
|
||||
|
||||
// 仅去掉末尾空白/换行(保留前导空白,避免“段落在完成时向左跳动”)
|
||||
static inline NSString *KBTrimRight(NSString *s) {
|
||||
if (s.length == 0) return s;
|
||||
NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
|
||||
NSInteger end = (NSInteger)s.length - 1;
|
||||
while (end >= 0 && [set characterIsMember:[s characterAtIndex:(NSUInteger)end]]) {
|
||||
end--;
|
||||
}
|
||||
if (end < 0) return @"";
|
||||
return [s substringToIndex:(NSUInteger)end + 1];
|
||||
}
|
||||
|
||||
#pragma mark - Public API
|
||||
|
||||
// 边输边见:实时更新当前 label 文本,遇到分隔符就新建下一个 label
|
||||
@@ -98,14 +112,11 @@
|
||||
self.labels.lastObject.text = self.buffer; // 不裁剪,保留实时输入
|
||||
[self layoutLabelsForCurrentWidth];
|
||||
|
||||
// 2) 处理每一个分隔符:完成当前段(可选裁剪)并新建空标签,然后填入下一段的内容
|
||||
// 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;
|
||||
}
|
||||
if (self.shouldTrimSegments) { current.text = KBTrimRight(current.text ?: @""); }
|
||||
[self layoutLabelsForCurrentWidth];
|
||||
|
||||
// b) 新建一个空标签,代表下一个段(即刻创建,哪怕下一段当前为空)
|
||||
@@ -133,9 +144,7 @@
|
||||
#pragma mark - Layout Helpers
|
||||
|
||||
- (void)addLabelForText:(NSString *)text {
|
||||
if (self.shouldTrimSegments) {
|
||||
text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
||||
}
|
||||
if (self.shouldTrimSegments) { text = KBTrimRight(text ?: @""); }
|
||||
// 创建一个可换行、可点击的 UILabel
|
||||
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
|
||||
label.numberOfLines = 0;
|
||||
@@ -195,7 +204,7 @@
|
||||
UILabel *current = self.labels.lastObject;
|
||||
if (!current) { return; }
|
||||
if (self.shouldTrimSegments) {
|
||||
NSString *trimmed = [current.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
||||
NSString *trimmed = KBTrimRight(current.text ?: @"");
|
||||
current.text = trimmed;
|
||||
self.buffer = trimmed;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user