PDFKit Text Widget Annotation - Resize while typing

Hi all,


I am working with PDFKit's PDFAnnotation and I am using the text widget, everything is working fine, but I am super stuck on one thing. I noticed in the native PDFKit on my iPhone (Open a PDF on your phone, tap the icon on the top right, then tap the plus icon on the bottom right and select Text) It will add a text field to the PDF, when I tap on the text, the edit option appears, when I start editing the whole text is highlighted and then if I start typing, the text field gets bigger in width, if I goto a new line it get bigger in height. My question is how would I do that part? Make text field bigger in width or height pending on the text size as I keep typing?


let page = pdfView.currentPage
           
textAnnotation = PDFAnnotation(bounds: CGRect(x: 550, y: 600, width: 600, height: 100), forType: PDFAnnotationSubtype(rawValue: PDFAnnotationSubtype.widget.rawValue), withProperties: nil)
           
textAnnotation.widgetFieldType = PDFAnnotationWidgetSubtype(rawValue: PDFAnnotationWidgetSubtype.text.rawValue)
           
textAnnotation.hasComb = true
           
textAnnotation.font = UIFont.systemFont(ofSize: 80)
           
textAnnotation.fontColor = .red
           
textAnnotation.isMultiline = true
           
textAnnotation.widgetStringValue = "Text Here"
           
pdfView.addGestureRecognizer(textGestureRecognizer)
           
textGestureRecognizer.textGestureRecognizerDelegate = self
           
page!.addAnnotation(textAnnotation)


I really hope this makes sense as I have been struggling with this issue for days now. PLEASE HELP! How do I get the text widget to get bigger as I type? It just gets cut off becuase of the bounds I set for the Annotation

Replies

I saw the question in SO and other places. Doesn't the proposed solution work ?

https://stackoverflow.com/questions/57114909/swift-4-pdfannotation-text-or-uitextfield-to-be-the-size-of-the-text


It is true that documentation is totally inexistant on this !

isMultiline should normally be enough.


Did you try setting some other properties, as:

var maximumLength: Int

Or reset the annotation rect ?


Note: why do you set so large a font (80) ?

The proposed solution is for UITextFields. I have tried maximumLength that didnt really do anything just limit the amount of characters I can have. Would a custom class help?


https://forums.developer.apple.com/message/332566#332566



"Or reset the annotation rect ?" - How can I trigger that while typing?

Not sure how to do this.


I would have thought there is a delegate to set, but cannot find any for annotations.

Maybe (sorry, I'm just gusssing), you could test cyclically the text annotation length in the controller, then adjust its size if you compute it is exceeding width. But that does not cope with multiline issue.

I played with text annotation in the Preview app (MacOS).

When you type initially, bow gros exactly to fit the text size, but text does not wrap.

It will wrap if you select the text tool and adjust font size for instance.


Could you make a test in your code by changing:

     textAnnotation.widgetStringValue = "Text Here: some very long text to test if text will wrap automatically with the multiline option set to true."

From this, another hint: what about setting the width of rect to zero initially. Maybe that will force it to grow automatically ? That's just an intuition, not tested at all.


I think you could file a bug report agains PDFKit documentation which is really deficient.

If I set the width to 0 initally, nothing appears at all.

Also


textAnnotation.widgetStringValue = "Text Here: some very long text to test if text will wrap automatically with the multiline option set to true."


Did not work, the text gets cut off due the width of my annotation.

Dear jsuke
Did you get any solution regarding this issue? I am also working in pdf annotation and I am also facing this issue. if u got anything, can you share here?

Thanks in Advance