The following snippets assume you are using a PTDocumentController
called documentController
:
Obj-C Swift
1 PTDocumentController * documentController = [[PTDocumentController alloc ] init ];
1 let documentController = PTDocumentController ()
Obj-C Swift
1 documentController.toolGroupIndicatorView.hidden = YES ;
1 documentController.toolGroupIndicatorView.isHidden = true
The toolbar can be programmatically hidden by setting the mode to view group, which is a special group and the only group where the toolbar is hidden:
Obj-C Swift
1 documentController.toolGroupManager.selectedGroup = documentController.toolGroupManager.viewItemGroup;
1 documentController.toolGroupManager.selectedGroup = documentController.toolGroupManager.viewItemGroup
The toolbar can be shown again by changing the group to any group other than view:
Obj-C Swift
1 documentController.toolGroupManager.selectedGroup = documentController.toolGroupManager.drawItemGroup;
1 documentController.toolGroupManager.selectedGroup = documentController.toolGroupManager.drawItemGroup
Toolbars can be removed by removing them from the toolGroupManager's groups array. The following code removes the "Draw" and "Pens" toolbars:
Obj-C Swift
1 NSMutableArray < PTToolItemGroup *>* mutableGroups = [documentController.toolGroupManager.groups mutableCopy ];
2
3 [mutableGroups removeObjectsInArray :@[documentController.toolGroupManager.drawItemGroup, documentController.toolGroupManager.pensItemGroup]];
4
5 documentController.toolGroupManager.groups = [mutableGroups copy ];
1 var mutableGroups = documentController.toolGroupManager.groups
2
3 let groupsToRemove = [documentController.toolGroupManager.drawItemGroup, documentController.toolGroupManager.pensItemGroup]
4
5 mutableGroups. removeAll ( where : {groupsToRemove. contains ( $0 )})
6
7 documentController.toolGroupManager.groups = mutableGroups
The example below shows how to remove the text highlight and text underline button from a toolbar.
Disabling a tool type entirely If you want to disable a tool entirely, from all toolbars and the long press menu, please use the annotations permissions system.
Obj-C Swift
1 PTToolItemGroup * annotateGroup = documentController.toolGroupManager.annotateItemGroup;
2
3 // tool buttons that exist currently
4 NSArray < UIBarButtonItem *>* defaultAnnotateGroupTools = annotateGroup.barButtonItems;
5
6 // new set of tools to replace current ones
7 NSMutableArray < UIBarButtonItem *>* newAnnotateGroupTools = [[ NSMutableArray alloc ] init ];
8
9 // add all currently existing tools except for the ones we don't want
10 for (UIBarButtonItem * defaultToolItem in defaultAnnotateGroupTools)
11 {
12 if ( [defaultToolItem isKindOfClass :[PTToolBarButtonItem class ]] )
13 {
14 PTToolBarButtonItem * toolBarButton = (PTToolBarButtonItem * )defaultToolItem;
15
16 if ( toolBarButton.toolClass == [PTTextHighlightCreate class ] ||
17 toolBarButton.toolClass == [PTTextUnderlineCreate class ] )
18 {
19 // do not add this tool
20 continue ;
21 }
22 else
23 {
24 [newAnnotateGroupTools addObject :defaultToolItem];
25 }
26 }
27 else
28 {
29 [newAnnotateGroupTools addObject :defaultToolItem];
30 }
31 }
32
33 // assign tools to new array
34 documentController.toolGroupManager.annotateItemGroup.barButtonItems = [newAnnotateGroupTools copy ];
1 let annotateGroup = documentController.toolGroupManager.annotateItemGroup
2
3 // tool buttons that exist currently
4 let defaultAnnotateGroupTools = annotateGroup.barButtonItems
5
6 // new set of tools to replace current ones
7 var newAnnotateGroupTools = [UIBarButtonItem]()
8
9 // add all currently existing tools except for the ones we don't want
10 for defaultToolItem in defaultAnnotateGroupTools
11 {
12 if defaultToolItem. isKind ( of : PTToolBarButtonItem. self ) {
13 let toolBarButton = defaultToolItem as! PTToolBarButtonItem
14 if toolBarButton.toolClass == PTTextHighlightCreate. self ||
15 toolBarButton.toolClass == PTTextUnderlineCreate. self
16 {
17 // do not add this tool
18 continue
19 }
20 else
21 {
22 newAnnotateGroupTools. append (toolBarButton)
23 }
24 }
25 else
26 {
27 newAnnotateGroupTools. append (defaultToolItem)
28 }
29 }
30
31 // assign tools to new array
32 documentController.toolGroupManager.annotateItemGroup.barButtonItems = newAnnotateGroupTools
Obj-C Swift
1 // create a mutable array of the current items in the annotation toolbar group
2 NSMutableArray < UIBarButtonItem *>* availableTools = [documentController.toolGroupManager.annotateItemGroup.barButtonItems mutableCopy ];
3
4 // create a new toolbar item for freehand annotations
5 UIBarButtonItem * freeHandItem = [documentController.toolGroupManager createItemForToolClass :[PTFreeHandCreate class ]];
6
7 // add the freehand annotation item to the front of the list
8 [availableTools insertObject :freeHandItem atIndex : 0 ];
9
10 // assign the array back to the annotation toolbar group.
11 documentController.toolGroupManager.annotateItemGroup.barButtonItems = [availableTools copy ];
1 // create a mutable array of the current items in the annotation toolbar group
2 var availableTools:[UIBarButtonItem] = documentController.toolGroupManager.annotateItemGroup.barButtonItems
3
4 // create a new toolbar item for freehand annotations
5 let freeHandItem = documentController.toolGroupManager. createItem ( forToolClass : PTFreeHandCreate. self )
6
7 // add the freehand annotation item to the front of the list
8 availableTools. insert (freeHandItem, at : 0 )
9
10 // assign the array back to the annotation toolbar group.
11 documentController.toolGroupManager.annotateItemGroup.barButtonItems = availableTools
The code below creates a new toolbar that contains a free hand, cloudy and image stamp tool.
Obj-C Swift
1 // the image that will be used in the toolbar switcher menu
2 UIImage * image = [UIImage systemImageNamed : @" square.and.pencil " ];
3
4 // the tools it will contain
5 UIBarButtonItem * freeHandItem = [documentController.toolGroupManager createItemForToolClass :[PTFreeHandCreate class ]];
6 UIBarButtonItem * cloudyItem = [documentController.toolGroupManager createItemForToolClass :[PTCloudCreate class ]];
7 UIBarButtonItem * stampItem = [documentController.toolGroupManager createItemForToolClass :[PTImageStampCreate class ]];
8
9 // the name of the custom group, its image, and its tool items
10 PTToolItemGroup * customGroup = [PTToolItemGroup groupWithTitle : @" MyApps Group " image :image barButtonItems :@[freeHandItem, cloudyItem, stampItem]];
11
12 // add the tool group
13 documentController.toolGroupManager.groups = [documentController.toolGroupManager.groups arrayByAddingObject :customGroup];
1 // the image that will be used in the toolbar switcher menu
2 let image = UIImage ( systemName : " square.and.pencil " )
3
4 // the tools it will contain
5 let freeHandItem = documentController.toolGroupManager. createItem ( forToolClass :PTFreeHandCreate. self )
6 let cloudyItem = documentController.toolGroupManager. createItem ( forToolClass :PTCloudCreate. self )
7 let stampItem = documentController.toolGroupManager. createItem ( forToolClass :PTImageStampCreate. self )
8
9 // the name of the custom group, its image, and its tool items
10 let customGroup:PTToolGroup = PTToolGroup ( title : " MyApps Group " , image : image, barButtonItems : [freeHandItem, cloudyItem, stampItem])
11
12 // add the tool group
13 documentController.toolGroupManager.groups. append (customGroup)
Your app may need a button that does not invoke one of the built in annotation tools. The following code will add a button that calls a selector.
Obj-C Swift
1 PTSelectableBarButtonItem * selectableItem = [[PTSelectableBarButtonItem alloc ] initWithImage :image style :UIBarButtonItemStylePlain target : self action : @ selector ( customToolAction: )];
2 selectableItem.title = @" Custom Tool " ;
3
4 documentController.toolGroupManager.annotateItemGroup.barButtonItems = [documentController.toolGroupManager.annotateItemGroup.barButtonItems arrayByAddingObject :selectableItem];
1 let selectableItem = PTSelectableBarButtonItem ( image : UIImage ( systemName : " square.and.pencil " ), style : .plain, target : self , action : #selector ( customToolAction (_:)))
2 selectableItem.title = " Custom Tool "
3
4 documentController.toolGroupManager.annotateItemGroup.barButtonItems. append (selectableItem)
If you want to toggle the items selection, flip its selected property:
Obj-C Swift
1 - ( void )customToolAction:(PTSelectableBarButtonItem * )button
2 {
3 button.selected = ! button.selected;
4 }
1 @objc func customToolAction ( _ button :PTSelectableBarButtonItem)
2 {
3 button.isSelected = ! button.isSelected
4 }
You can modify the navigation bar items. Here is an example of adding a new button (for the current size class):
Obj-C Swift
1 UIBarButtonItem * myItem = [[UIBarButtonItem alloc ] initWithImage :[UIImage systemImageNamed : @" square.and.pencil " ] style :UIBarButtonItemStylePlain target : nil action : nil ];
2
3 documentController.navigationItem.rightBarButtonItems = [documentController.navigationItem.rightBarButtonItems arrayByAddingObject :myItem];
1 let myItem = UIBarButtonItem ( image : UIImage ( systemName : " square.and.pencil " ), style : .plain, target : nil , action : nil )
2
3 documentController.navigationItem.rightBarButtonItems. append (myItem)
Tools can also be added:
Obj-C Swift
1 UIBarButtonItem * freeHand = [documentController.toolGroupManager createItemForToolClass :[PTFreeHandCreate class ]];
2
3 documentController.navigationItem.rightBarButtonItems = [documentController.navigationItem.rightBarButtonItems arrayByAddingObject :freeHand];
1 let freeHand = documentController.toolGroupManager. createItem ( forToolClass :PTFreeHandCreate. self )
2
3 documentController.navigationItem.rightBarButtonItems. append (freeHand)
Note that in the example above, de-selecting the tool button item needs to be implemented by the app, by listening to the Tool Did Change notification.
These are the buttons that appear at the bottom of the screen.
Obj-C Swift
1 // new button
2 UIBarButtonItem * myItem = [[UIBarButtonItem alloc ] initWithImage :[UIImage systemImageNamed : @" square.and.pencil " ] style :UIBarButtonItemStylePlain target : nil action : nil ];
3
4 // spacer to keep evenly spaced buttons
5 UIBarButtonItem * spacer = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem :UIBarButtonSystemItemFlexibleSpace target : nil action : nil ];
6
7 // new array
8 NSMutableArray * toolbarItems = [documentController.toolbarItems mutableCopy ];
9
10 // add the new items
11 [toolbarItems addObjectsFromArray :@[spacer, myItem]];
12
13 // set the toolbarItems to the new items
14 documentController.toolbarItems = [toolbarItems copy ];
1 // new button
2 let myItem = UIBarButtonItem ( image : UIImage ( systemName : " square.and.pencil " ), style : .plain, target : nil , action : nil )
3
4 // spacer to keep evenly spaced buttons
5 let spacer = UIBarButtonItem ( barButtonSystemItem : .flexibleSpace, target : nil , action : nil )
6
7 // new array
8 var toolbarItems = documentController.toolbarItems
9
10 // add the new items
11 toolbarItems. append ( contentsOf : [spacer, myItem])
12
13 // set the toolbarItems to the new items
14 documentController.toolbarItems = toolbarItems