Annotation Inspector

The Annotation Inspector (PSPDFAnnotationStyleViewController) is a PSPDFKit UI component that allows you to change the different properties of an annotation. It allows you to customize the selected annotations, by changing their various appearance properties.

You can also customize the annotation inspector by providing only a limited set of annotation properties to the users for customization.

The set of customizable properties can be found in PSPDFAnnotationStyleKey.h.

Customize Annotation Inspector Properties

To change the properties available in the style inspector, simply set the propertiesForAnnotations property of the PSPDFConfigurationBuilder as follows:

Copy
1
2
3
4
5
6
7
8
9
10
let configuration = PSPDFConfiguration { builder in
  var annotationProperties = builder.propertiesForAnnotations

  annotationProperties[.ink] = [[AnnotationStyleKey.lineWidth, AnnotationStyleKey.color]]
  annotationProperties[.underline] = [[AnnotationStyleKey.alpha]]
  annotationProperties[.line] = [[AnnotationStyleKey.color, AnnotationStyleKey.lineEnd1, AnnotationStyleKey.lineEnd2]]
  annotationProperties[.square] = [[AnnotationStyleKey.color, AnnotationStyleKey.fillColor]]

  builder.propertiesForAnnotations = annotationProperties
}
Copy
1
2
3
4
5
6
7
8
9
10
PSPDFConfiguration *configuration = [PSPDFConfiguration configurationWithBuilder: ^(PSPDFConfigurationBuilder *builder) {
  NSMutableDictionary *annotationProperties = [builder.propertiesForAnnotations mutableCopy];

  annotationProperties[PSPDFAnnotationStringInk] = @[@[PSPDFAnnotationStyleKeyLineWidth, PSPDFAnnotationStyleKeyColor]];
  annotationProperties[PSPDFAnnotationStringUnderline] = @[@[PSPDFAnnotationStyleKeyAlpha]];
  annotationProperties[PSPDFAnnotationStringLine] = @[@[PSPDFAnnotationStyleKeyColor, PSPDFAnnotationStyleKeyLineEnd1, PSPDFAnnotationStyleKeyLineEnd2]];
  annotationProperties[PSPDFAnnotationStringSquare] = @[@[PSPDFAnnotationStyleKeyColor, PSPDFAnnotationStyleKeyFillColor]];

  builder.propertiesForAnnotations = annotationProperties;
}];

This can also be achieved by sublclassing the PSPDFAnnotationStyleViewController and overriding the propertiesForAnnotations: method to provide the properties for the different annotation types.

Copy
1
2
3
4
5
6
7
8
9
10
override func properties(for annotations: [PSPDFAnnotation]) -> [[AnnotationStyleKey]] {
    let defaultSections = super.properties(for: annotations)
    // Allow only a smaller list of known properties in the inspector popover.
    let supportedKeys: Set<AnnotationStyleKey> = [.color, .alpha, .lineWidth, .fontSize]
    return defaultSections.map { propertiesInSection in
        return propertiesInSection.filter { property in
            return supportedKeys.contains(property)
        }
    }
}
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (NSArray<NSArray<PSPDFAnnotationStyleKey> *> *)propertiesForAnnotations:(NSArray<PSPDFAnnotation *> *)annotations {
    NSArray<NSArray<PSPDFAnnotationStyleKey> *> *defaultSections = [super propertiesForAnnotations:annotations];

    // Allow only a smaller list of known properties in the inspector popover.
    NSSet<PSPDFAnnotationStyleKey> *supportedKeys = [NSSet setWithObjects: PSPDFAnnotationStyleKeyColor, PSPDFAnnotationStyleKeyAlpha, PSPDFAnnotationStyleKeyLineWidth, PSPDFAnnotationStyleKeyFontSize, nil];

    NSMutableArray<NSArray<PSPDFAnnotationString> *> *newSections = [NSMutableArray array];
    for (NSArray *properties in defaultSections) {
        [newSections addObject:[properties filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString *property, NSDictionary *bindings2) {
            return [supportedKeys containsObject:property];
        }]]];
    }
    return newSections;
}

Take a look at PSCSimpleAnnotationInspectorExample.m in the PSPDFCatalog for a complete example.

The propertiesForAnnotations also accepts a block that takes a PSPDFAnnotation and returns an array of array of PSPDFAnnotationStyleKey. This is useful when you want to customize the properties for a given annotation variant.

Copy
1
2
3
4
5
6
7
8
9
// We need to explicityly use @convention(block) as the Array is `id` typed.
typealias AnnotationStyleBlock = @convention(block) (PSPDFAnnotation) -> [[AnnotationStyleKey]]
let styleBlock: AnnotationStyleBlock = { annotation in
    return [[AnnotationStyleKey.color, AnnotationStyleKey.fillColor]]
}
// Casting swift closure to AnyObject since closures in swift are not directly converted to objective-c closures.
let blockObject = unsafeBitCast(styleBlock, to: AnyObject.self)
annotationProperties[.ink] = blockObject

Copy
1
2
3
4
PSPDFAnnotationStyleBlock styleBlock = ^PSPDFAnnotationStyleKeyGroupedList (PSPDFAnnotation *annotation) {
    return @[@[PSPDFAnnotationStyleKeyColor, PSPDFAnnotationStyleKeyFillColor]];
};
annotationProperties[PSPDFAnnotationStringSquare] = styleBlock;

To customize the Color Presets in the style inspector, check our Customizing Color Presets guide.