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
public func setupDefaultStylesIfNeeded() {
  // Line widths
  setLastUsedValue(4, forProperty: "lineWidth", forKey: .ink)
  setLastUsedValue(4, forProperty: "lineWidth", forKey: PSPDFAnnotationStateVariantIdentifier(.ink, .inkVariantPen))
  setLastUsedValue(30, forProperty: "lineWidth", forKey: PSPDFAnnotationStateVariantIdentifier(.ink, .inkVariantHighlighter))
  setLastUsedValue(5, forProperty: "lineWidth", forKey: .square)
  setLastUsedValue(5, forProperty: "lineWidth", forKey: .circle)
  setLastUsedValue(3, forProperty: "lineWidth", forKey: .polygon)
  setLastUsedValue(3, forProperty: "lineWidth", forKey: .line)
  setLastUsedValue(4, forProperty: "lineWidth", forKey: PSPDFAnnotationStateVariantIdentifier(.line, .lineVariantArrow))
  setLastUsedValue(3, forProperty: "lineWidth", forKey: .polyLine)
  setLastUsedValue(3, forProperty: "lineWidth", forKey: .signature)
  setLastUsedValue(10, forProperty: "lineWidth", forKey: .eraser)

  // 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: .highlight)
  setLastUsedValue(UIColor.black, forProperty: "color", forKey: .underline)
  setLastUsedValue(UIColor.red, forProperty: "color", forKey: .squiggly)
  setLastUsedValue(UIColor.red, forProperty: "color", forKey: .strikeOut)

  // Set ink and variants
  setLastUsedValue(drawingBlue, forProperty: "color", forKey: .ink)
  setLastUsedValue(drawingBlue, forProperty: "color", forKey: PSPDFAnnotationStateVariantIdentifier(.ink, .inkVariantPen))
  setLastUsedValue(UIColor.yellow, forProperty: "color", forKey: PSPDFAnnotationStateVariantIdentifier(.ink, .inkVariantHighlighter))
  setLastUsedValue(0.5, forProperty: "alpha", forKey: PSPDFAnnotationStateVariantIdentifier(.ink, .inkVariantHighlighter))

  setLastUsedValue(drawingBlue, forProperty: "color", forKey: .square)
  setLastUsedValue(drawingBlue, forProperty: "color", forKey: .circle)
  setLastUsedValue(drawingBlue, forProperty: "color", forKey: .polygon)
  setLastUsedValue(drawingBlue, forProperty: "color", forKey: .line)
  setLastUsedValue(UIColor.red, forProperty: "color", forKey: PSPDFAnnotationStateVariantIdentifier(.line, .lineVariantArrow))
  setLastUsedValue(drawingBlue, forProperty: "color", forKey: .polyLine)
  setLastUsedValue(UIColor.black, forProperty: "color", forKey: .signature)
  setLastUsedValue(UIColor.black, forProperty: "color", forKey: .freeText)

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

  // Fonts
  setLastUsedValue(12, forProperty: "fontSize", forKey: .freeText)
  setLastUsedValue("Helvetica", forProperty: "fontName", forKey: .freeText)
}
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
- (void)setupDefaultStylesIfNeeded {
  // Line widths
  [self setLastUsedValue:@(4) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStringInk];
  [self setLastUsedValue:@(4) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStateVariantIdentifier(PSPDFAnnotationStringInk, PSPDFAnnotationStringInkVariantPen)];
  [self setLastUsedValue:@(30) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStateVariantIdentifier(PSPDFAnnotationStringInk, PSPDFAnnotationStringInkVariantHighlighter)];
  [self setLastUsedValue:@(5) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStringSquare];
  [self setLastUsedValue:@(5) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStringCircle];
  [self setLastUsedValue:@(3) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStringPolygon];
  [self setLastUsedValue:@(3) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStringLine];
  [self setLastUsedValue:@(4) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStateVariantIdentifier(PSPDFAnnotationStringLine, PSPDFAnnotationStringLineVariantArrow)];
  [self setLastUsedValue:@(3) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStringPolyLine];
  [self setLastUsedValue:@(3) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStringSignature];
  [self setLastUsedValue:@(10) forProperty:PROPERTY(lineWidth) forKey:PSPDFAnnotationStringEraser];

  // 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:PSPDFAnnotationStringHighlight];
  [self setLastUsedValue:UIColor.blackColor forProperty:PROPERTY(color) forKey:PSPDFAnnotationStringUnderline];
  [self setLastUsedValue:UIColor.redColor forProperty:PROPERTY(color) forKey:PSPDFAnnotationStringSquiggly];
  [self setLastUsedValue:UIColor.redColor forProperty:PROPERTY(color) forKey:PSPDFAnnotationStringStrikeOut];

  // Set ink and variants
  [self setLastUsedValue:drawingBlue forProperty:PROPERTY(color) forKey:PSPDFAnnotationStringInk];
  [self setLastUsedValue:drawingBlue forProperty:PROPERTY(color) forKey:PSPDFAnnotationStateVariantIdentifier(PSPDFAnnotationStringInk, PSPDFAnnotationStringInkVariantPen)];
  [self setLastUsedValue:UIColor.yellowColor forProperty:PROPERTY(color) forKey:PSPDFAnnotationStateVariantIdentifier(PSPDFAnnotationStringInk, PSPDFAnnotationStringInkVariantHighlighter)];
  [self setLastUsedValue:@(0.5f) forProperty:PROPERTY(alpha) forKey:PSPDFAnnotationStateVariantIdentifier(PSPDFAnnotationStringInk, PSPDFAnnotationStringInkVariantHighlighter)];

  [self setLastUsedValue:drawingBlue forProperty:PROPERTY(color) forKey:PSPDFAnnotationStringSquare];
  [self setLastUsedValue:drawingBlue forProperty:PROPERTY(color) forKey:PSPDFAnnotationStringCircle];
  [self setLastUsedValue:drawingBlue forProperty:PROPERTY(color) forKey:PSPDFAnnotationStringPolygon];
  [self setLastUsedValue:drawingBlue forProperty:PROPERTY(color) forKey:PSPDFAnnotationStringLine];
  [self setLastUsedValue:UIColor.redColor forProperty:PROPERTY(color) forKey:PSPDFAnnotationStateVariantIdentifier(PSPDFAnnotationStringLine, PSPDFAnnotationStringLineVariantArrow)];
  [self setLastUsedValue:drawingBlue forProperty:PROPERTY(color) forKey:PSPDFAnnotationStringPolyLine];
  [self setLastUsedValue:UIColor.blackColor forProperty:PROPERTY(color) forKey:PSPDFAnnotationStringSignature];
  [self setLastUsedValue:UIColor.blackColor forProperty:PROPERTY(color) forKey:PSPDFAnnotationStringFreeText];

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

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

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
/// 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.
public func setLastUsedValue(_ value: Any?, forProperty styleProperty: String, forKey key: PSPDFAnnotationString)
Copy
1
2
3
4
5
/// 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.
- (void)setLastUsedValue:(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: .ink)
PSPDFKit.sharedInstance.styleManager.setLastUsedValue(drawingColor, forProperty: colorProperty, forKey: PSPDFAnnotationStateVariantIdentifier(.ink, .inkVariantPen))

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

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

// Set line width of highlight annotations
PSPDFKit.sharedInstance.styleManager.setLastUsedValue(20, forProperty: lineWidthProperty, forKey: PSPDFAnnotationStateVariantIdentifier(.ink, .inkVariantHighlighter))
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:PSPDFAnnotationStringInk];
[[PSPDFKit sharedInstance].styleManager setLastUsedValue:drawingColor forProperty:colorProperty forKey:PSPDFAnnotationStateVariantIdentifier(PSPDFAnnotationStringInk, PSPDFAnnotationStringInkVariantPen)];

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

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

// Set line width of highlight annotations
[[PSPDFKit sharedInstance].styleManager setLastUsedValue:@(20) forProperty:lineWidthProperty forKey:PSPDFAnnotationStateVariantIdentifier(PSPDFAnnotationStringInk, PSPDFAnnotationStringInkVariantHighlighter)];
Was this page helpful? We're happy to answer any questions.