How to update toolbar button color

Hi @Jamie_Dassoulas,

You have provided a good solution for the PTFreeHandCreate tool code. We want to make it dynamic for any tool. and we are trying this with the following approach.

How can we get the tool name from annotation (PTAnnot)? [line no 152 in img]

(Our purpose: How to update toolbar button image color?
on changing annotation color. tool of that annotation should change to the same color.)

Thanks.

Hi @rankit.agarwal,

I think you could use the following (this code is Objective-C but you should be able to use the same APIs in swift):

- (void)toolManager:(PTToolManager *)toolManager annotationModified:(PTAnnot *)annotation onPageNumber:(unsigned long)pageNumber
{
    for (PTToolGroup *group in self.toolGroupManager.groups) {
        for (UIBarButtonItem *item in group.barButtonItems) {
            if ([item isKindOfClass:[PTToolBarButtonItem class]]) {
                PTToolBarButtonItem *toolItem = (PTToolBarButtonItem *)item;
                if ([toolItem.toolClass annotType] == [annotation extendedAnnotType]) {
                    PTAnnotStyle *style = [[PTAnnotStyle allocOverridden] initWithAnnot:annotation];
                    [toolItem setImageStyle:style];
                }
            }
        }
    }
}
1 Like

Yes it worked thanks @Jamie_Dassoulas

Hi @Jamie_Dassoulas
We have implemented as you suggested,

   func toolManager(_ toolManager: PTToolManager, annotationModified annotation: PTAnnot, onPageNumber pageNumber: UInt) {
        setToolbarColorForAnnotation(annotation)
    }
    
    func setToolbarColorForAnnotation(_ annot: PTAnnot){
        
        let drawclass = documentController.toolGroupManager.annotateItemGroup.barButtonItems;
        
        let tool = drawclass?.first(where: { button in
            (button as! PTToolBarButtonItem).toolClass?.annotType == annot.extendedAnnotType
        })

        let style = PTAnnotStyle(annotType: annot.extendedAnnotType)
        (tool as! PTToolBarButtonItem).setImageStyle(style)
            
    }

But after adding callout annotation, the app is getting freeze when moving it.
If we comment out this function it is working fine, Can you please check and help is it correct implementation?

Hi @rankit.agarwal,

We have reproduced this and are working on a fix.

Hi @Jamie_Dassoulas ,

→ This toolbar color change is also causing issues in Text Annotations, Can you please check that also?

→ And also like android, is it possible for you to handle the code from the library side like this? [How to update toolbar button image color? - #7 by Shirley_Gong]

Thanks.

Hi @rankit.agarwal,

We have a fix for the callout and text issue available for testing in this build:

DMG:
https://nightly-pdftron.s3-us-west-2.amazonaws.com/experimental/2022-05-17/PDFNet_iOS_nightly_developer_2022-05-17_experimental.dmg

CocoaPods:

https://nightly-pdftron.s3.amazonaws.com/experimental/2022-05-17/cocoapods/xcframeworks/pdfnet/2022-05-17_experimental_rev79832.podspec

Are you able to try that and let us know if it works?

Hi @Jamie_Dassoulas ,

In this build, the toolbar color is changing but next time same new color is not been selected for annotation.

Attaching video for better understaning.

Hi @Jamie_Dassoulas ,
Any update on this? Kindly update

Hi @rankit.agarwal,

Unfortunately I haven’t been able to reproduce this, do you have some more customizations in your code?
Attached is a video showing my test as well as the ViewController.swift file I used.

ViewController.swift (2.3 KB)

Hi @Jamie_Dassoulas ,

We are using
(tool as! PTToolBarButtonItem).setImageStyle(style)

and in your vc it is
(tool as! PTToolBarButtonItem).setImageFor(style)

setImageFor is not available in latest experimental build version: 9.3079841
Can you share the link again which version to try?

Hi @rankit.agarwal,

Apologies, that was just a copy and paste error from referencing an older version of the API.
(tool as! PTToolBarButtonItem).setImageStyle(style) is the correct implementation.

I just tested again with this and am still unable to reproduce the issue.

Hi @Jamie_Dassoulas ,

We replace our code with your given view controller but still it is not working.

Just to confirm, we are downloading SDK from: PDFTron Systems Inc. | Nightly

Device - iPad Air 4th gen simulator
os version - 15.4
Video:


Version :

Can you please again share the SDK link which we can try for this and the whole project zip file?

Hi @rankit.agarwal,

Are you able to try using this nightly build:

CocoaPods Link

DMG Download

My ViewController.swift can just be used to replace the ViewController in the SwiftSample in the DMG download.

Hi @Jamie_Dassoulas

We are using Dynamic PDFnet XCFframework from above DMG mentioned URL: GitHub - saikat90/PDFSwiftSample

Steps to run the project:

Tools XCFramework are already there in the Github URL
PDFNet XCFramework we need to download from the above DMG mentioned URL

Result:
For highlighter Markup, it is not working for us

Let us know
What are we doing wrong?

Hi Saikat,

Thank you for your feedback. Can you please try the attached code to see if it works? PTAnnotEditTool and PTToolGroupToolbar are overridden.

Thank you,
Yifan

ViewController.swift (3.3 KB)

After adding this view controller in the above project facing a crash on GroupToolbar class initialization
PFA

ViewController.swift (3.5 KB)

Can you please help me out with this?

Thanks
Saikat

Hi Saikat,

You could try adding this in GroupToolbar class:

override init(frame: CGRect) {
    super.init(frame: frame)
}

Please let us know how this works for you.

Thank you,
Yifan

1 Like

Awesome, the Crash got resolved.
It is working for us.
Will play around more on this and let you know.

Thanks Yifan

Hi @Yifan_Zhang ,

With this solution, we are facing one issue. If we change the color of the annotation and at that time if another tool is selected its icon is also getting changed. Attaching a video for better understanding.

Steps to reproduce:

  • Draw any markup. eg. using a highlighter.
  • Select the Callout markup tool from toolbar.
  • Select drawn highlighter markup and change the color of that annotation.
  • Callout markup and highlighter both tool’s icon and color are getting change. It should change only the highlighter tool icon.

This is happening with all tools. Can you please help us with how we can fix that?

Thanks.


Controller file:
ViewController.swift (4.8 KB)