Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand iOS sample app #31

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions Projects/NeonExample-iOS/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,22 @@ final class ViewController: UIViewController {
.appendingPathComponent("queries/highlights.scm")
let query = try! language.query(contentsOf: url!)

let attrProvider: TextViewSystemInterface.AttributeProvider = { token in
guard token.name.hasPrefix("keyword") else { return [:] }

return [.foregroundColor: UIColor.red]
let regularFont = UIFont.monospacedSystemFont(ofSize: 16, weight: .regular)
let boldFont = UIFont.monospacedSystemFont(ofSize: 16, weight: .bold)
let italicFont = regularFont.fontDescriptor.withSymbolicTraits(.traitItalic).map { UIFont(descriptor: $0, size: 16) } ?? regularFont

let provider: TextViewSystemInterface.AttributeProvider = { token in
return switch token.name {
case let keyword where keyword.hasPrefix("keyword"): [.foregroundColor: UIColor.red, .font: boldFont]
case "comment": [.foregroundColor: UIColor.green, .font: italicFont]
default: [.foregroundColor: UIColor.darkText, .font: regularFont]
}
}

return try! TextViewHighlighter(textView: textView,
language: language,
highlightQuery: query,
attributeProvider: attrProvider)
attributeProvider: provider)
}()

override func viewDidLoad() {
Expand All @@ -33,7 +39,11 @@ final class ViewController: UIViewController {
_ = highlighter.textView
_ = textView.layoutManager

textView.text = "var something = String()"
textView.text = """
// Example Code!
let value = "hello world"
print(value)
"""

self.view.addSubview(textView)
textView.translatesAutoresizingMaskIntoConstraints = false
Expand Down
Loading