### Swift Package Installation for HighlighterSwift Source: https://context7.com/smittytone/highlighterswift/llms.txt Integrates HighlighterSwift into your project using Swift Package Manager. This involves adding the repository URL to your package dependencies in the Package.swift file. It specifies the package name, supported platforms, and the Highlighter dependency. ```swift import PackageDescription let package = Package( name: "MyApp", platforms: [ .macOS(.v11), .iOS(.v13), .tvOS(.v13), .visionOS(.v1) ], dependencies: [ .package(url: "https://github.com/smittytone/HighlighterSwift.git", from: "3.0.0") ], targets: [ .target( name: "MyApp", dependencies: ["Highlighter"] ) ] ) ``` -------------------------------- ### Add Line Numbers to Code Snippets Source: https://context7.com/smittytone/highlighterswift/llms.txt Demonstrates how to use the LineNumberData structure to prepend line numbers to highlighted code. This is intended for presentation purposes and allows customization of starting numbers, width, and separators. ```swift if let highlighter = Highlighter() { highlighter.setTheme("github-dark", withFont: "Menlo", ofSize: 14.0) let code = """ import Foundation struct User { let name: String let email: String } let user = User(name: \"John\", email: \"john@example.com\") print(user) \"\"" var lineNumbering = LineNumberData() lineNumbering.numberStart = 1 lineNumbering.minWidth = 3 lineNumbering.separator = " | " lineNumbering.usingDarkTheme = true lineNumbering.fontSize = 14.0 if let numberedCode = highlighter.highlight(code, as: "swift", lineNumbering: lineNumbering) { myTextView.textStorage?.setAttributedString(numberedCode) } var snippetNumbering = LineNumberData() snippetNumbering.numberStart = 42 snippetNumbering.minWidth = 4 snippetNumbering.usingDarkTheme = false if let snippetCode = highlighter.highlight("let x = 10", as: "swift", lineNumbering: snippetNumbering) { } } ``` -------------------------------- ### Highlight Code String in HighlighterSwift Source: https://github.com/smittytone/highlighterswift/blob/main/README.md Get an optional NSAttributedString containing the formatted code. The second parameter specifies the language; if omitted or nil, Highlight.js's language detection will be used. ```swift if let displayString: NSAttributedString = highlighter.highlight(codeString, as: "swift") { myTextView.textStorage!.addAttributedString(displayString) } ``` -------------------------------- ### Configure Line Numbering Data Structure Source: https://github.com/smittytone/highlighterswift/blob/main/README.md Defines the LineNumberData struct used to customize line numbering behavior, including start values, width, separators, and theme settings. This structure is passed to the highlight method for presentation purposes. ```swift public struct LineNumberData { public var numberStart: Int = 1 public var minWidth: Int = 2 public var separator: String = " " public var usingDarkTheme: Bool = false public var lineBreak: String = "\n" public var fontSize: CGFloat = 16.0 } ``` -------------------------------- ### Initialize Highlighter Instance Source: https://context7.com/smittytone/highlighterswift/llms.txt Demonstrates how to instantiate the Highlighter class. The initializer is failable and returns nil if the bundled resources are missing. ```swift import Highlighter if let highlighter = Highlighter() { print("Highlighter ready with default theme") } else { print("Failed to initialize Highlighter") } ``` -------------------------------- ### Configure Highlighter Themes Source: https://context7.com/smittytone/highlighterswift/llms.txt Shows how to apply different themes and customize font settings. The setTheme method allows specifying a theme name, font family, and size. ```swift if let highlighter = Highlighter() { highlighter.setTheme("atom-one-dark") highlighter.setTheme("dracula", withFont: "Menlo-Regular", ofSize: 16.0) highlighter.setTheme("github", withFont: "SF Mono") let success = highlighter.setTheme("monokai") if !success { print("Theme 'monokai' not found") } } ``` -------------------------------- ### Initialize HighlighterSwift Source: https://github.com/smittytone/highlighterswift/blob/main/README.md Instantiate a Highlighter object. The init() function returns an optional, which will be nil if the Highlight.min.js file or the Default theme CSS file is missing or non-functional. ```swift if let highlighter = Highlighter() { // Highlighter initialized successfully } ``` -------------------------------- ### Observe Theme Changes Source: https://context7.com/smittytone/highlighterswift/llms.txt Demonstrates setting up a closure to observe when the theme is updated, allowing for dynamic UI updates. ```swift if let highlighter = Highlighter() { highlighter.themeChanged = { newTheme in print("Theme changed!") #if os(macOS) myTextView.backgroundColor = newTheme.themeBackgroundColour! #endif } highlighter.setTheme("monokai", withFont: "Fira Code", ofSize: 15.0) } ``` -------------------------------- ### List Available Themes and Languages Source: https://context7.com/smittytone/highlighterswift/llms.txt Shows how to retrieve and iterate through the collections of supported themes and programming languages provided by the library. ```swift if let highlighter = Highlighter() { let themes = highlighter.availableThemes() for theme in themes.sorted() { print(" - \(theme)") } let languages = highlighter.supportedLanguages() for lang in languages.sorted() { print(" - \(lang)") } } ``` -------------------------------- ### Access Theme Background and Properties Source: https://context7.com/smittytone/highlighterswift/llms.txt Explains how to extract the theme's background color and check if the theme is dark to adjust UI elements accordingly. ```swift if let highlighter = Highlighter() { highlighter.setTheme("dracula") let backgroundColor = highlighter.theme.themeBackgroundColour #if os(macOS) myTextView.backgroundColor = backgroundColor! #else myTextView.backgroundColor = backgroundColor #endif let isDark = highlighter.theme.isDark } ``` -------------------------------- ### Apply Line Numbering to Highlighted Code Source: https://github.com/smittytone/highlighterswift/blob/main/README.md Demonstrates how to instantiate LineNumberData, configure its properties, and pass it to the highlighter.highlight method to generate an NSAttributedString with line numbers. ```swift var lineNumberingData = LineNumberData() lineNumberingData.minWidth = 4 lineNumberingData.numberStart = 100 lineNumberingData.usingDarkTheme = !isMacInLightMode() lineNumberingData.fontSize = self.settings.fontSize if let displayString: NSAttributedString = highlighter.highlight(codeString, as: "swift", lineNumbering: lineNumberingData) { myTextView.textStorage!.addAttributedString(displayString) } ``` -------------------------------- ### Ignore Illegal Syntax Source: https://context7.com/smittytone/highlighterswift/llms.txt Shows how to enable the ignoreIllegals property to ensure highlighting continues even when invalid syntax is encountered. ```swift if let highlighter = Highlighter() { highlighter.setTheme("github") highlighter.ignoreIllegals = true let malformedCode = "function( { incomplete syntax" if let result = highlighter.highlight(malformedCode, as: "javascript") { myTextView.textStorage?.setAttributedString(result) } } ``` -------------------------------- ### Highlight Source Code Source: https://context7.com/smittytone/highlighterswift/llms.txt Converts raw source code strings into styled NSAttributedString objects. Supports explicit language specification or automatic detection. ```swift if let highlighter = Highlighter() { highlighter.setTheme("atom-one-light", withFont: "Menlo-Regular", ofSize: 14.0) let swiftCode = "func greet(name: String) -> String {\n return \"Hello, \\(name)!\"\n}\n\nlet message = greet(name: \"World\")\nprint(message)" if let attributedCode = highlighter.highlight(swiftCode, as: "swift") { myTextView.textStorage?.setAttributedString(attributedCode) } let unknownCode = "console.log('Hello');" if let autoDetected = highlighter.highlight(unknownCode) { myTextView.textStorage?.setAttributedString(autoDetected) } } ``` -------------------------------- ### Configure Spacing in HighlighterSwift Source: https://github.com/smittytone/highlighterswift/blob/main/README.md Set line spacing and paragraph spacing values for the theme. Both values must be non-negative; negative values will be replaced with defaults. A line spacing of 0.0 is equivalent to single spacing. ```swift highlighter.theme.lineSpacing = self.lineSpacing * self.fontSize ``` ```swift highlighter.theme.paraSpacing = 1.0 ``` -------------------------------- ### Set Theme and Font in HighlighterSwift Source: https://github.com/smittytone/highlighterswift/blob/main/README.md Set a specific theme and optionally apply a custom font with a specified size. If no font is provided, it defaults to 14pt Courier. ```swift highlighter.setTheme("atom-one-light") ``` ```swift highlighter.setTheme("atom-one-light", withFont: "Menlo-Regular", ofSize: 16.0) ``` -------------------------------- ### Apply Custom Fonts to Theme Source: https://context7.com/smittytone/highlighterswift/llms.txt Updates the code font dynamically using platform-specific font objects (NSFont for macOS, UIFont for iOS). ```swift import Highlighter #if os(macOS) import AppKit #else import UIKit #endif if let highlighter = Highlighter() { highlighter.setTheme("solarized-dark") #if os(macOS) if let customFont = NSFont(name: "JetBrains Mono", size: 15.0) { highlighter.theme.setCodeFont(customFont) } #else if let customFont = UIFont(name: "JetBrains Mono", size: 15.0) { highlighter.theme.setCodeFont(customFont) } #endif } ``` -------------------------------- ### Adjust Line and Paragraph Spacing Source: https://context7.com/smittytone/highlighterswift/llms.txt Configures text layout properties within the theme to control spacing between lines and paragraphs. ```swift if let highlighter = Highlighter() { highlighter.setTheme("nord", withFont: "Monaco", ofSize: 13.0) highlighter.theme.lineSpacing = 4.0 highlighter.theme.paraSpacing = 8.0 let code = "let x = 1\nlet y = 2\n\nlet z = x + y" if let styledCode = highlighter.highlight(code, as: "swift") { myTextView.textStorage?.setAttributedString(styledCode) } } ``` -------------------------------- ### Set Code Font in HighlighterSwift Source: https://github.com/smittytone/highlighterswift/blob/main/README.md Set or change the preferred code font later using setCodeFont(). This method takes an NSFont or UIFont instance configured for the desired font and text size. ```swift NSFont font = NSFont(name: "Menlo-Regular", size: 12.0) ?? NSFont.systemFont(ofSize: 12.0) highlighter.theme.setCodeFont(font) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.