This commit is contained in:
2025-11-12 15:31:22 +08:00
parent 1dbe04cdf9
commit c317afc0fe
3 changed files with 53 additions and 44 deletions

View File

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