YYText icon indicating copy to clipboard operation
YYText copied to clipboard

YYLayout layoutWithContainer related API issue

Open Kyle-Ye opened this issue 7 months ago • 0 comments

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

Kyle-Ye avatar Jun 25 '24 04:06 Kyle-Ye