How to update toolbar button color

Product: PDFtron

Product Version: 9.2

Please give a brief summary of your issue:

  1. How do we update the toolbar item image color, which matches the color selected for tool items like ARROW, LINE, PEN tool, etc?

We also want to do similar to Android and Web, we want to hide our preset and make the last selected color in the toolbar tool color, Is there any way to do that? (Currently selected color from preset is setting as tool color)

Reference Video and Ticket:
(How to update toolbar button image color? - #3 by emallon)

  1. For hiding preset we are using
    documentController.toolGroupToolbar.isPresetsToolbarEnabled = false
    but it is hiding presets only for iPhone. We also tried isPresetsToolbarHidden but that is also not working. How can we hide preset tools from the toolbar on iPad and iPhone both?

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:APIs:Forums:

Hi,

To hide presets on iPad, you could override PTToolGroupToolbar and in the subclass, add:

- (void)setPresetsViewHidden:(BOOL)hidden
{
    [super setPresetsViewHidden:YES];
}

- (void)setPresetsViewHidden:(BOOL)hidden animated:(BOOL)animated
{
    [super setPresetsViewHidden:YES animated:animated];
}

We are investigating an approach for you to update toolbar button colour and will let you know when we have an update.

Thank you,
Yifan

Hi @Yifan_Zhang ,

Can you please share how can we do that?
documentController.toolGroupToolbar = GroupToolbar()
we are getting toolGroupToolbar is get only property error.

class GroupToolbar: PTToolGroupToolbar {
    override func  setPresetsViewHidden(_ hidden: Bool, animated: Bool) {
    super.setPresetsViewHidden(true, animated: animated)
}

We have create subclass like this to and now we are not able to assign it to toolGroupToolbar. Can you please share the example.

Hi,

Here is an example to override a PDFTron class with a subclass (PTOverrides Class Reference):

PTOverrides.overrideClass(PTToolGroupToolbar.self, with: GroupToolbar.self)

This should be called before documentController is created.

Best,
Yifan

1 Like

Hi @rankit.agarwal,

We recently released v9.2.2 of the PDFTron iOS SDK and we added an API to set the style (colours) of the icon of a PTToolBarButtonItem:
e.g.

// Get the array of items in the default Draw toolbar
NSArray *drawItems = documentController.toolGroupToolbar.toolGroupManager.drawItemGroup.barButtonItems;

// Find the first ink tool item
PTToolBarButtonItem *inkItem = [drawItems objectAtIndex:[drawItems indexOfObjectPassingTest:^BOOL(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    return ((PTToolBarButtonItem*)obj).toolClass == [PTFreeHandCreate class];
}]];
// Create a new PTAnnotStyle and set its stroke color
PTAnnotStyle *style = [[PTAnnotStyle alloc] initWithAnnotType:PTExtendedAnnotTypeInk];
[style setStrokeColor:UIColor.greenColor];

// Set the style of the item's image
[inkItem setImageStyle:style];

Please let us know if you are able to test it out.

Are there any other remaining issues for which you need guidance or assistance?

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.