Toggle PDF Form Field Highlight Color in Swift for iOS

Shows how to toggle the drawing of the form highlight color on or off. Get additional resources by visiting our PSPDFFormElement API guide.


//
// Copyright © 2016-2025 PSPDFKit GmbH. All rights reserved.
//
// The Nutrient sample applications are licensed with a modified BSD license.
// Please see License for details. This notice may not be removed from this file.
//
import PSPDFKit
import PSPDFKitUI
class FormHighlightExample: Example {
private weak var pdfController: PDFViewController?
override init() {
super.init()
title = "Custom Form Highlight Color"
contentDescription = "Shows how to toggle the form highlight color."
category = .viewCustomization
}
override func invoke(with delegate: ExampleRunnerDelegate) -> UIViewController {
let document = AssetLoader.document(for: "Form.pdf")
// Start by not highlighting forms.
document.updateRenderOptions(for: .all) {
$0.interactiveFormFillColor = .clear
}
let image = SDK.imageNamed("highlight.png")!
let toggleButton = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(toggleHighlight))
let pdfController = PDFViewController(document: document)
pdfController.navigationItem.rightBarButtonItems = [toggleButton]
self.pdfController = pdfController
return pdfController
}
// MARK: Actions
@objc func toggleHighlight() {
guard let pdfController = self.pdfController, let document = pdfController.document else {
return
}
// Toggle between highlighted forms and clear forms.
let currentColor = document.renderOptions(forType: .all).interactiveFormFillColor
let highlightColor = UIColor.catalogAccent.withAlphaComponent(0.2)
document.updateRenderOptions(for: .page) { options in
options.interactiveFormFillColor = currentColor == .clear ? highlightColor : .clear
}
pdfController.reloadData()
}
}

This code sample is an example that illustrates how to use our SDK. Please adapt it to your specific use case.