Configure annotation presets on iOS

Nutrient iOS SDK has default values for some annotation properties (for example, colors and line widths for ink annotations). It sometimes also has multiple styles per annotation type (“variants”).

This is all handled in AnnotationStyleManager, which is a global singleton. You can access it with SDK.shared.styleManager.

The current set of defaults is configured on the first run and subsequently saved in UserDefaults:

public func setupDefaultStylesIfNeeded() {
// Line widths.
setLastUsedValue(4, forProperty: "lineWidth", forKey: Annotation.ToolVariantID(tool: .ink))
setLastUsedValue(4, forProperty: "lineWidth", forKey: Annotation.ToolVariantID(tool: .ink, variant: .inkPen))
setLastUsedValue(30, forProperty: "lineWidth", forKey: Annotation.ToolVariantID(tool: .ink, variant: .inkHighlighter))
setLastUsedValue(5, forProperty: "lineWidth", forKey: Annotation.ToolVariantID(tool: .square))
setLastUsedValue(5, forProperty: "lineWidth", forKey: Annotation.ToolVariantID(tool: .circle))
setLastUsedValue(3, forProperty: "lineWidth", forKey: Annotation.ToolVariantID(tool: .polygon))
setLastUsedValue(3, forProperty: "lineWidth", forKey: Annotation.ToolVariantID(tool: .polygon, variant: .cloud))
setLastUsedValue(3, forProperty: "lineWidth", forKey: Annotation.ToolVariantID(tool: .line))
setLastUsedValue(4, forProperty: "lineWidth", forKey: Annotation.ToolVariantID(tool: .line, variant: .lineArrow))
setLastUsedValue(3, forProperty: "lineWidth", forKey: Annotation.ToolVariantID(tool: .polyLine))
setLastUsedValue(3, forProperty: "lineWidth", forKey: Annotation.ToolVariantID(tool: .signature))
setLastUsedValue(10, forProperty: "lineWidth", forKey: Annotation.ToolVariantID(tool: .eraser))
// Colors.
let black = UIColor.black
let blue = UIColor(red: 0.141, green: 0.573, blue: 0.984, alpha: 1) // #2492FB
let red = UIColor(red: 0.871, green: 0.278, blue: 0.31, alpha: 1) // #DE474F
let yellow = UIColor(red: 0.996, green: 0.91, blue: 0.196, alpha: 1) // #FEE832
setLastUsedValue(yellow, forProperty: "color", forKey: Annotation.ToolVariantID(tool: .highlight))
setLastUsedValue(black, forProperty: "color", forKey: Annotation.ToolVariantID(tool: .underline))
setLastUsedValue(red, forProperty: "color", forKey: Annotation.ToolVariantID(tool: .squiggly))
setLastUsedValue(red, forProperty: "color", forKey: Annotation.ToolVariantID(tool: .strikeOut))
// Set ink and variants.
setLastUsedValue(blue, forProperty: "color", forKey: Annotation.ToolVariantID(tool: .ink))
setLastUsedValue(blue, forProperty: "color", forKey: Annotation.ToolVariantID(tool: .ink, variant: .inkMagic))
setLastUsedValue(blue, forProperty: "color", forKey: Annotation.ToolVariantID(tool: .ink, variant: .inkPen))
setLastUsedValue(yellow, forProperty: "color", forKey: Annotation.ToolVariantID(tool: .ink, variant: .inkHighlighter))
setLastUsedValue(0.5, forProperty: "alpha", forKey: Annotation.ToolVariantID(tool: .ink, variant: .inkHighlighter))
setLastUsedValue(blue, forProperty: "color", forKey: Annotation.ToolVariantID(tool: .square))
setLastUsedValue(blue, forProperty: "color", forKey: Annotation.ToolVariantID(tool: .circle))
setLastUsedValue(blue, forProperty: "color", forKey: Annotation.ToolVariantID(tool: .polygon))
setLastUsedValue(blue, forProperty: "color", forKey: Annotation.ToolVariantID(tool: .polygon, variant: .cloud))
setLastUsedValue(blue, forProperty: "color", forKey: Annotation.ToolVariantID(tool: .line))
setLastUsedValue(red, forProperty: "color", forKey: Annotation.ToolVariantID(tool: .line, variant: .lineArrow))
setLastUsedValue(blue, forProperty: "color", forKey: Annotation.ToolVariantID(tool: .polyLine))
setLastUsedValue(black, forProperty: "color", forKey: Annotation.ToolVariantID(tool: .signature))
setLastUsedValue(black, forProperty: "color", forKey: Annotation.ToolVariantID(tool: .freeText))
setLastUsedValue(red, forProperty: "color", forKey: Annotation.ToolVariantID(tool: .redaction))
// Arrow style.
setLastUsedValue(AbstractLineAnnotation.EndType.openArrow.rawValue, forProperty: "lineEnd2", forKey: Annotation.ToolVariantID(tool: .line, variant: .lineArrow))
// Fonts.
setLastUsedValue(12, forProperty: "fontSize", forKey: Annotation.ToolVariantID(tool: .freeText))
setLastUsedValue("Helvetica", forProperty: "fontName", forKey: Annotation.ToolVariantID(tool: .freeText))
// Border effect.
setLastUsedValue(Annotation.BorderEffect.noEffect, forProperty: "borderEffect", forKey: Annotation.ToolVariantID(tool: .circle))
setLastUsedValue(Annotation.BorderEffect.noEffect, forProperty: "borderEffect", forKey: Annotation.ToolVariantID(tool: .square))
setLastUsedValue(Annotation.BorderEffect.noEffect, forProperty: "borderEffect", forKey: Annotation.ToolVariantID(tool: .polygon))
setLastUsedValue(Annotation.BorderEffect.cloudy, forProperty: "borderEffect", forKey: Annotation.ToolVariantID(tool: .polygon, variant: .cloud))
setLastUsedValue(2, forProperty: "borderEffectIntensity", forKey: Annotation.ToolVariantID(tool: .polygon, variant: .cloud))
// Blend mode.
setLastUsedValue(CGBlendMode.multiply.rawValue, forProperty: "blendMode", forKey: Annotation.ToolVariantID(tool: .highlight))
setLastUsedValue(CGBlendMode.multiply.rawValue, forProperty: "blendMode", forKey: Annotation.ToolVariantID(tool: .ink, variant: .inkHighlighter))
// Fill color.
setLastUsedValue(black, forProperty: "fillColor", forKey: Annotation.ToolVariantID(tool: .redaction))
// Outline color.
setLastUsedValue(red, forProperty: "outlineColor", forKey: Annotation.ToolVariantID(tool: .redaction))
setLastUsedValue(nil, forProperty: "overlayText", forKey: Annotation.ToolVariantID(tool: .redaction))
setLastUsedValue(false, forProperty: "repeatOverlayText", forKey: Annotation.ToolVariantID(tool: .redaction))
}

You can use this call somewhere in your code to update these defaults. We suggest you add a custom key in NSUserDefaults and store your overrides under it so that they aren’t mixed with the changes to the default values that users make:

/**
Convenience method. Will set the last used style for `key` and `styleProperty`.
- parameter value: might be a boxed `CGFloat`, color, or whatever matches the property.
`styleProperty` is the `NSString` name for the property (e.g. `NSStringFromSelector(@ selector(fontSize))`.
`key` is an annotation string, e.g. `AnnotationStringFreeText`.
Uses `AnnotationStyle.Kind.lastUsed` and calls `add(_:forKey:)`.
*/
public func setLastUsedValue(_ value: Any?, forProperty styleProperty: String, forKey key: Annotation.ToolVariantID)

Example

Here’s how to set a few properties of ink and highlight annotations:

let drawingColor = UIColor.green
let highlightingColor = UIColor.red
let colorProperty = "color"
let alphaProperty = "alpha"
let lineWidthProperty = "lineWidth"
SDK.shared.styleManager.setLastUsedValue(drawingColor, forProperty: colorProperty, forKey: Annotation.ToolVariantID(tool: .ink))
SDK.shared.styleManager.setLastUsedValue(drawingColor, forProperty: colorProperty, forKey: Annotation.ToolVariantID(tool: .ink, variant: .inkPen))
// Set highlight color.
SDK.shared.styleManager.setLastUsedValue(highlightingColor, forProperty: colorProperty, forKey: Annotation.ToolVariantID(tool: .ink, variant: .inkHighlighter))
SDK.shared.styleManager.setLastUsedValue(0.5, forProperty: alphaProperty, forKey: Annotation.ToolVariantID(tool: .ink, variant: .inkHighlighter))
// Set line width of ink annotations.
SDK.shared.styleManager.setLastUsedValue(5, forProperty: lineWidthProperty, forKey: Annotation.ToolVariantID(tool: .ink))
SDK.shared.styleManager.setLastUsedValue(5, forProperty: lineWidthProperty, forKey: Annotation.ToolVariantID(tool: .ink, variant: .inkPen))
// Set line width of highlight annotations.
SDK.shared.styleManager.setLastUsedValue(20, forProperty: lineWidthProperty, forKey: Annotation.ToolVariantID(tool: .ink, variant: .inkHighlighter))