ibireme / YYText

Powerful text framework for iOS to display and edit rich text.
MIT License
8.84k stars 1.67k forks source link

YYLayout layoutWithContainer related API issue #1004

Open Kyle-Ye opened 2 weeks ago

Kyle-Ye commented 2 weeks ago
  1. Swift API issue

On Swift side, the signature of +[YYTextLayout layoutWithContainer:text:] and +[YYTextLayout layoutWithContainers:text:] would both be YYTextLayout.layout(with:text:) automatically.

We need some manually NS_SWIFT_NAME to fix it.

Saying it, it is now will call the +[YYTextLayout layoutWithContainers:text:] API and we should workaround it by using YYTextLayout.layout(with:[container], text: text)[0].

But it will bring out to the issue 2

  1. layoutWithContainers empty array issue

I'll always get an empty array for a valid input. Debugging the source code, it looks like YYTextLayout forgot to add layout into the layouts array.

+ (NSArray *)layoutWithContainers:(NSArray *)containers text:(NSAttributedString *)text range:(NSRange)range {
    if (!containers || !text) return nil;
    if (range.location + range.length > text.length) return nil;
    NSMutableArray *layouts = [NSMutableArray array];
    for (NSUInteger i = 0, max = containers.count; i < max; i++) {
        YYTextContainer *container = containers[i];
        YYTextLayout *layout = [self layoutWithContainer:container text:text range:range];
        if (!layout) return nil;
+       [layouts addObject:layout]; 
        NSInteger length = (NSInteger)range.length - (NSInteger)layout.visibleRange.length;
        if (length <= 0) {
            range.length = 0;
            range.location = text.length;
        } else {
            range.length = length;
            range.location += layout.visibleRange.length;
        }
    }
    return layouts;
}