CIAttributedTextImageGenerator - multiline text image generation problem in iOS 16

123 Views Asked by At

I am usinig CIAttributedTextImageGenerator to generate text into CIImage in my app.

let attributes: [NSAttributedString.Key: Any] = [
    .font: font,
    .foregroundColor: textColor,
    .backgroundColor: textBackgroundColor,
    .shadow: shadow,
    .strokeColor: strokeColor,
    .strokeWidth: strokeWidth
]

let attributedQuote = NSAttributedString(string: text, attributes: attributes)

let textGenerationFilter = CIFilter(name: "CIAttributedTextImageGenerator")!
textGenerationFilter.setValue(attributedQuote, forKey: "inputText")
if #available(iOS 16.0, macCatalyst 16.0, macOS 13.0, tvOS 16.0, *) {
    textGenerationFilter.setValue(CGFloat(0), forKey: "inputPadding")
}
textGenerationFilter.setValue(NSNumber(value: Double(4.0)), forKey: "inputScaleFactor")

guard let textImage = textGenerationFilter.outputImage else {
    return nil
}

It was working perfectly until the release of iOS 16. In iOS 16 the behavior changed and now when I try to render a multiline text, the generated text image ends up being way too wide. With each additional line in the text, the width of generated image drastically increases for some reason. The height increases too, but that is obviously to fit the new text line but the width increases without any obvious reasons.

I attached screenshots of what is happening:

enter image description here

As can been seen, the single liners work perfectly. But with each additional line of text, the rendered image shrinks because the rendered image width becomes wider and wider and it has to shrink to follow the .aspectFit rule.

In the end, the width of the generated image becomes even more than its height, despite the text being clearly bigger in height than width.

That never happens on iOS 15 and the result is as expected there. So is there anything new I am missing either in CIAttributedTextImageGenerator or NSAttributedString introduced in iOS 16 that I am missing?

0

There are 0 best solutions below