Customize the Annotation Inspector on iOS

The annotation inspector (AnnotationStyleViewController) is a PSPDFKit UI component that allows you to change the different properties of an annotation. You can use it to customize selected annotations by changing their various appearance properties.

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

The AnnotationStyle.Key type includes the set of customizable properties.

Customize Annotation Inspector Properties

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

let configuration = PDFConfiguration { builder in
  var annotationProperties = builder.propertiesForAnnotations

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

  builder.propertiesForAnnotations = annotationProperties
}
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 subclassing AnnotationStyleViewController and overriding the properties(for:) method to provide the properties for the different annotation types:

override func properties(for annotations: [Annotation]) -> [[AnnotationStyle.Key]] {
    let defaultSections = super.properties(for: annotations)
    // Allow only a smaller list of known properties in the inspector popover.
    let supportedKeys: Set<AnnotationStyle.Key> = [.color, .alpha, .lineWidth, .fontSize]
    return defaultSections.map { propertiesInSection in
        return propertiesInSection.filter { property in
            return supportedKeys.contains(property)
        }
    }
}
- (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 PSPDFKit Catalog for a complete example.

propertiesForAnnotations also accepts a block that takes an Annotation and returns an array of AnnotationStyle.Keys. This is useful when you want to customize the properties for a given annotation variant:

// We need to explicitly use `@convention(block)`, as the `Array` is `id` typed.
typealias AnnotationStyleBlock = @convention(block) (Annotation) -> [[AnnotationStyle.Key]]
let styleBlock: AnnotationStyleBlock = { annotation in
    return [[AnnotationStyle.Key.color, AnnotationStyle.Key.fillColor]]
}
// Casting the 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
PSPDFAnnotationStyleBlock styleBlock = ^PSPDFAnnotationStyleKeyGroupedList (PSPDFAnnotation *annotation) {
    return @[@[PSPDFAnnotationStyleKeyColor, PSPDFAnnotationStyleKeyFillColor]];
};
annotationProperties[PSPDFAnnotationStringSquare] = styleBlock;

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