Changing Default Values for Color and Text Size of Annotations

PSPDFKit has default values for some of the annotation properties (e.g. colors and line widths for the ink annotation) - sometimes also multiple styles per annotation type ("variants").

This is all handled in the PSPDFAnnotationStyleManager which is a global singleton. You can access it with PSPDFKit.sharedInstance.styleManager.

The current set of defaults is configured on the first run and subsequently saved in NSUserDefaults.

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public func setupDefaultStylesIfNeeded() {
  // Line widths
  setLastUsedValue(4, forProperty: "lineWidth", forKey: PSPDFAnnotationStateVariantIDMake(.ink, nil))
  setLastUsedValue(4, forProperty: "lineWidth", forKey: PSPDFAnnotationStateVariantIDMake(.ink, .inkPen))
  setLastUsedValue(30, forProperty: "lineWidth", forKey: PSPDFAnnotationStateVariantIDMake(.ink, .inkHighlighter))
  setLastUsedValue(5, forProperty: "lineWidth", forKey: PSPDFAnnotationStateVariantIDMake(.square, nil))
  setLastUsedValue(5, forProperty: "lineWidth", forKey: PSPDFAnnotationStateVariantIDMake(.circle, nil))
  setLastUsedValue(3, forProperty: "lineWidth", forKey: PSPDFAnnotationStateVariantIDMake(.polygon, nil))
  setLastUsedValue(3, forProperty: "lineWidth", forKey: PSPDFAnnotationStateVariantIDMake(.line, nil))
  setLastUsedValue(4, forProperty: "lineWidth", forKey: PSPDFAnnotationStateVariantIDMake(.line, .lineArrow))
  setLastUsedValue(3, forProperty: "lineWidth", forKey: PSPDFAnnotationStateVariantIDMake(.polyLine, nil))
  setLastUsedValue(3, forProperty: "lineWidth", forKey: PSPDFAnnotationStateVariantIDMake(.signature, nil))
  setLastUsedValue(10, forProperty: "lineWidth", forKey: PSPDFAnnotationStateVariantIDMake(.eraser, nil))

  // Colors
  let highlightYellow = UIColor(red: 0.99, green: 0.93, blue: 0.55, alpha: 1)
  let drawingBlue = UIColor(red: 0.121, green: 0.35, blue: 1, alpha: 1)
  setLastUsedValue(highlightYellow, forProperty: "color", forKey: PSPDFAnnotationStateVariantIDMake(.highlight, nil))
  setLastUsedValue(UIColor.black, forProperty: "color", forKey: PSPDFAnnotationStateVariantIDMake(.underline, nil))
  setLastUsedValue(UIColor.red, forProperty: "color", forKey: PSPDFAnnotationStateVariantIDMake(.squiggly, nil))
  setLastUsedValue(UIColor.red, forProperty: "color", forKey: PSPDFAnnotationStateVariantIDMake(.strikeOut, nil))

  // Set ink and variants
  setLastUsedValue(drawingBlue, forProperty: "color", forKey: PSPDFAnnotationStateVariantIDMake(.ink, nil))
  setLastUsedValue(drawingBlue, forProperty: "color", forKey: PSPDFAnnotationStateVariantIDMake(.ink, .inkHighlighter))
  setLastUsedValue(UIColor.yellow, forProperty: "color", forKey: PSPDFAnnotationStateVariantIDMake(.ink, .inkHighlighter))
  setLastUsedValue(0.5, forProperty: "alpha", forKey: PSPDFAnnotationStateVariantIDMake(.ink, .inkHighlighter))

  setLastUsedValue(drawingBlue, forProperty: "color", forKey: PSPDFAnnotationStateVariantIDMake(.square, nil))
  setLastUsedValue(drawingBlue, forProperty: "color", forKey: PSPDFAnnotationStateVariantIDMake(.circle, nil))
  setLastUsedValue(drawingBlue, forProperty: "color", forKey: PSPDFAnnotationStateVariantIDMake(.polygon, nil))
  setLastUsedValue(drawingBlue, forProperty: "color", forKey: PSPDFAnnotationStateVariantIDMake(.line, nil))
  setLastUsedValue(UIColor.red, forProperty: "color", forKey: PSPDFAnnotationStateVariantIDMake(.line, .lineArrow))
  setLastUsedValue(drawingBlue, forProperty: "color", forKey: PSPDFAnnotationStateVariantIDMake(.polyLine, nil))
  setLastUsedValue(UIColor.black, forProperty: "color", forKey: PSPDFAnnotationStateVariantIDMake(.signature, nil))
  setLastUsedValue(UIColor.black, forProperty: "color", forKey: PSPDFAnnotationStateVariantIDMake(.freeText, nil))
  setLastUsedValue(UIColor(red: 1, green: 0.15, blue:0, alpha:1), forProperty: "color", forKey: PSPDFAnnotationStateVariantIDMake(.redaction, nil))

  // Arrow speciality
  setLastUsedValue(PSPDFLineEndType.openArrow, forProperty: "lineEnd2", forKey: PSPDFAnnotationStateVariantIDMake(.line, .lineArrow))

  // Fonts
  setLastUsedValue(12, forProperty: "fontSize", forKey: PSPDFAnnotationStateVariantIDMake(.freeText, nil))
  setLastUsedValue("Helvetica", forProperty: "fontName", forKey: PSPDFAnnotationStateVariantIDMake(.freeText, nil))

  // Fill color
  setLastUsedValue(UIColor.black, forProperty: "fillColor", forKey: PSPDFAnnotationStateVariantIDMake(.redaction, nil))

  // Outline color
  setLastUsedValue(UIColor(red: 1, green: 0.15, blue:0, alpha:1), forProperty: "outlineColor", forKey: PSPDFAnnotationStateVariantIDMake(.redaction, nil))
}
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
- (void)setupDefaultStylesIfNeeded {
  // Line widths
  [self setLastUsedValue:@(4) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringInk, nil)];
  [self setLastUsedValue:@(4) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringInk, PSPDFAnnotationVariantStringInkPen)];
  [self setLastUsedValue:@(30) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringInk, PSPDFAnnotationVariantStringInkHighlighter)];
  [self setLastUsedValue:@(5) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringSquare, nil)];
  [self setLastUsedValue:@(5) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringCircle, nil)];
  [self setLastUsedValue:@(3) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringPolygon, nil)];
  [self setLastUsedValue:@(3) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringLine, nil)];
  [self setLastUsedValue:@(4) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringLine, PSPDFAnnotationVariantStringLineArrow)];
  [self setLastUsedValue:@(3) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStateVariantIDMake((PSPDFAnnotationStringPolyLine, nil)];
  [self setLastUsedValue:@(3) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringSignature, nil)];
  [self setLastUsedValue:@(10) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringEraser, nil)];

  // Colors
  UIColor *highlightYellow = [UIColor colorWithRed:0.99f green:0.93f blue:0.55f alpha:1.f];
  UIColor *drawingBlue = [UIColor colorWithRed:0.121f green:0.35f blue:1.f alpha:1.f];
  [self setLastUsedValue:highlightYellow forProperty:PROPERTY(color) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringHighlight, nil)];
  [self setLastUsedValue:UIColor.blackColor forProperty:PROPERTY(color) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringUnderline, nil)];
  [self setLastUsedValue:UIColor.redColor forProperty:PROPERTY(color) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringSquiggly, nil)];
  [self setLastUsedValue:UIColor.redColor forProperty:PROPERTY(color) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringStrikeOut, nil)];

  // Set ink and variants
  [self setLastUsedValue:drawingBlue forProperty:PROPERTY(color) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringInk, nil)];
  [self setLastUsedValue:drawingBlue forProperty:PROPERTY(color) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringInk, PSPDFAnnotationVariantStringInkPen)];
  [self setLastUsedValue:UIColor.yellowColor forProperty:PROPERTY(color) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringInk, PSPDFAnnotationVariantStringInkHighlighter)];
  [self setLastUsedValue:@(0.5f) forProperty:PROPERTY(alpha) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringInk, PSPDFAnnotationVariantStringInkHighlighter)];

  [self setLastUsedValue:drawingBlue forProperty:PROPERTY(color) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringSquare, nil)];
  [self setLastUsedValue:drawingBlue forProperty:PROPERTY(color) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringCircle, nil)];
  [self setLastUsedValue:drawingBlue forProperty:PROPERTY(color) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringPolygon, nil)];
  [self setLastUsedValue:drawingBlue forProperty:PROPERTY(color) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringLine, nil)];
  [self setLastUsedValue:UIColor.redColor forProperty:PROPERTY(color) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringLine, PSPDFAnnotationVariantStringLineArrow)];
  [self setLastUsedValue:drawingBlue forProperty:PROPERTY(color) forKey:PSPDFAnnotationStateVariantIDMake((PSPDFAnnotationStringPolyLine, nil)];
  [self setLastUsedValue:UIColor.blackColor forProperty:PROPERTY(color) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringSignature, nil)];
  [self setLastUsedValue:UIColor.blackColor forProperty:PROPERTY(color) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringFreeText, nil)];
  [self setLastUsedValue:[UIColor colorWithRed:1.f green:0.15f blue:0.f alpha:1.f] forProperty:PROPERTY(color) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringRedaction, nil)];

  // Arrow speciality
  [self setLastUsedValue:@(PSPDFLineEndTypeOpenArrow) forProperty:PROPERTY(lineEnd2) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringLine, PSPDFAnnotationVariantStringLineArrow)];

  // Fonts
  [self setLastUsedValue:@(12) forProperty:PROPERTY(fontSize) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringFreeText, nil)];
  [self setLastUsedValue:@"Helvetica" forProperty:PROPERTY(fontName) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringFreeText, nil)];

  // Fill color
  [self setLastUsedValue:UIColor.blackColor forProperty:PROPERTY(fillColor) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringRedaction, nil)];

  // Outline color
  [self setLastUsedValue:[UIColor colorWithRed:1.f green:0.15f blue:0.f alpha:1.f] forProperty:PROPERTY(outlineColor) forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringRedaction, nil)];
}

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 are not mixed with the changes to the default values that the users make.

Copy
1
2
3
4
5
6
7
8
/**
  Convenience method. Will set the last used style for `key` and `styleProperty`.
  `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 a annotation string, e.g. PSPDFAnnotationStringFreeText.
  Uses `PSPDFStyleManagerLastUsedStylesKey` and calls `addStyle:forKey`.
*/
public func setLastUsedValue(_ value: Any?, forProperty styleProperty: String, forKey key: PSPDFAnnotationString)
Copy
1
2
3
4
5
6
7
8
/**
  Convenience method. Will set the last used style for `key` and `styleProperty`.
  `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 id constructed from annotation's type and variant using `PSPDFAnnotationStateVariantIDMake` function, e.g. PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringLine, PSPDFAnnotationVariantStringLineArrow).
  Uses `PSPDFStyleManagerLastUsedStylesKey` and calls `addStyle:forKey`.
*/
- (void)setLastUsedValue:(nullable id)value forProperty:(NSString *)styleProperty forKey:(PSPDFAnnotationString)key;

Example

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

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
let drawingColor = UIColor.green
let highlightingColor = UIColor.red
let colorProperty = "color"
let alphaProperty = "alpha"
let lineWidthProperty = "lineWidth"

// Set ink color
PSPDFKit.sharedInstance.styleManager.setLastUsedValue(drawingColor, forProperty: colorProperty, forKey: PSPDFAnnotationStateVariantIDMake(.ink, nil))
PSPDFKit.sharedInstance.styleManager.setLastUsedValue(drawingColor, forProperty: colorProperty, forKey: PSPDFAnnotationStateVariantIDMake(.ink, .inkPen))

// Set highlight color
PSPDFKit.sharedInstance.styleManager.setLastUsedValue(highlightingColor, forProperty: colorProperty, forKey: PSPDFAnnotationStateVariantIDMake(.ink, .inkHighlighter))
PSPDFKit.sharedInstance.styleManager.setLastUsedValue(0.5, forProperty: alphaProperty, forKey: PSPDFAnnotationStateVariantIDMake(.ink, .inkHighlighter))

// Set line width of ink annotations
PSPDFKit.sharedInstance.styleManager.setLastUsedValue(5, forProperty: lineWidthProperty, forKey: PSPDFAnnotationStateVariantIDMake(.ink, nil))
PSPDFKit.sharedInstance.styleManager.setLastUsedValue(5, forProperty: lineWidthProperty, forKey: PSPDFAnnotationStateVariantIDMake(.ink, .inkPen))

// Set line width of highlight annotations
PSPDFKit.sharedInstance.styleManager.setLastUsedValue(20, forProperty: lineWidthProperty, forKey: PSPDFAnnotationStateVariantIDMake(.ink, .inkHighlighter))
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
UIColor *drawingColor = [UIColor greenColor];
UIColor *highlightingColor = [UIColor redColor];
NSString *colorProperty = NSStringFromSelector(@selector(color));
NSString *alphaProperty = NSStringFromSelector(@selector(alpha));
NSString *lineWidthProperty = NSStringFromSelector(@selector(lineWidth));

// Set ink color
[[PSPDFKit sharedInstance].styleManager setLastUsedValue:drawingColor forProperty:colorProperty forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringInk, nil)];
[[PSPDFKit sharedInstance].styleManager setLastUsedValue:drawingColor forProperty:colorProperty forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringInk, PSPDFAnnotationVariantStringInkPen)];

// Set highlight color
[[PSPDFKit sharedInstance].styleManager setLastUsedValue:highlightingColor forProperty:colorProperty forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringInk, PSPDFAnnotationVariantStringInkHighlighter)];
[[PSPDFKit sharedInstance].styleManager setLastUsedValue:@(0.5f) forProperty:alphaProperty forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringInk, PSPDFAnnotationVariantStringInkHighlighter)];

// Set line width of ink annotations
[[PSPDFKit sharedInstance].styleManager setLastUsedValue:@(5) forProperty:lineWidthProperty forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringInk, nil)];
[[PSPDFKit sharedInstance].styleManager setLastUsedValue:@(5) forProperty:lineWidthProperty forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringInk, PSPDFAnnotationVariantStringInkPen)];

// Set line width of highlight annotations
[[PSPDFKit sharedInstance].styleManager setLastUsedValue:@(20) forProperty:lineWidthProperty forKey:PSPDFAnnotationStateVariantIDMake(PSPDFAnnotationStringInk, PSPDFAnnotationVariantStringInkHighlighter)];