Customize the Annotation Inspector on iOS

The annotation inspector (AnnotationStyleViewController(opens in a new tab)) 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(opens in a new tab) property of the PDFConfigurationBuilder(opens in a new tab) 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
}

This can also be achieved by subclassing AnnotationStyleViewController(opens in a new tab) and overriding the properties(for:)(opens in a new tab) 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)
}
}
}

Take a look at PSCSimpleAnnotationInspectorExample.m in PSPDFKit Catalog for a complete example.

propertiesForAnnotations(opens in a new tab) 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

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