Skip to content

Commit

Permalink
2.0 (#42)
Browse files Browse the repository at this point in the history
* Renames editor source files since UIKit can run on macOS

* Removes previous modifiers

* Removes modifier updates

* Restores onSelectionChange

* Moves init args to modifiers

* Removes old modifiers

* Deletes wrapper class init vars

* Moves init args to modifiers

* Access rules

* Introspect fn

* Makes internal vars public

* UIKit moves editor to textView sub-var to match AppKit

* Removes static vars from HighlightedTextEditor

* All preset

* Renames class var to customTextView

* Custom text view var is not recreated every change

* Removes unused accessors for modifiers

* Removes unused text var

* Workaround for introspect bug

* Workaround

* Passes accessible data out of classes as struct

* Updates example code to show new modifiers

* Updates API for new modifiers

* Cleanup

* SwiftFormat config

* SwiftFormat on project

* Linting

* xcode profile

* Removes test code for removed modifiers

* Team

* Removes iOS tests for removed editor functionality

* Removes unused AppKit tests

* New editor to test modifiers

* e2e tests new modifiers

* New Introspect test editor

* Removes the initialization of two properties that didn't need it

* Introspect works on subsequent updates in UIKit editor

* Introspect modifier gets updates with AppKit

* Spacing

* Moves internals inside HLTE struct

* References right project

* e2e tests introspect modifier

* e2e tests introspect

* Documents regex preset

* UIKit tests run on iPhone 11

* Removes unused snapshots

* Renames font enum option

* Aliases callbacks

* Renames to ScrollableTextView

* Moves internal editor class inside HLTE to de-pollute global namespace

* Makes internal classes final

* Does not make unnecessary vars on redraw

* Credit comments

* Updates iOS snapshots

* Resets to main branch

* Removes unused system extensions

* Linting stage of CI

* Lint fixes

* Removes Install homebrew step since GHA already has it

* Removes swiftlint
  • Loading branch information
kyle-n authored May 27, 2021
1 parent 710bb6f commit e7f0db3
Show file tree
Hide file tree
Showing 44 changed files with 877 additions and 1,122 deletions.
14 changes: 12 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ env:

jobs:

linting:
name: Linting
runs-on: macos-latest

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Lint project
run: swiftlint

appkit:
name: AppKit Tests
runs-on: macos-latest
Expand Down Expand Up @@ -71,7 +81,7 @@ jobs:
sed -i '' "s/kyle-n/${GITHUB_ACTOR//\//\\/}/g" $XCODEPROJ
- name: Boot Simulator
env:
IOS_SIM_NAME: iPhone 11
IOS_SIM_NAME: iPhone 12
run: |
IOS_SIM_UDID=`xcrun simctl list | grep -w "$IOS_SIM_NAME" | awk 'match($0, /\(([-0-9A-F]+)\)/) { print substr( $0, RSTART + 1, RLENGTH - 2 )}' | head -1`
SIMULATOR_PATH='/Applications/Xcode.app/Contents/Developer/Applications/Simulator.app/Contents/MacOS/Simulator'
Expand All @@ -91,7 +101,7 @@ jobs:
- name: Run UIKit tests
run: |
xcodebuild -resolvePackageDependencies -project "$TEST_PROJECT_PATH"
xcodebuild test -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 11' -scheme "$TEST_PROJECT_NAME (iOS)" -project $TEST_PROJECT_PATH
xcodebuild test -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 12' -scheme "$TEST_PROJECT_NAME (iOS)" -project $TEST_PROJECT_PATH
killall Simulator
- name: Save non-matching iOS snapshots
if: failure()
Expand Down
15 changes: 15 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--swiftversion 5
--binarygrouping none
--commas inline
--decimalgrouping none
--hexgrouping none
--ifdef no-indent
--nospaceoperators ..<
--octalgrouping none
--self init-only
--semicolons never
--stripunusedargs closure-only
--wraparguments before-first
--wrapcollections before-first
--wrapparameters before-first
--maxwidth 120
7 changes: 4 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

Expand All @@ -12,11 +11,13 @@ public let package = Package(
products: [
.library(
name: "HighlightedTextEditor",
targets: ["HighlightedTextEditor"]),
targets: ["HighlightedTextEditor"]
)
],
targets: [
.target(
name: "HighlightedTextEditor",
dependencies: [])
dependencies: []
)
]
)
76 changes: 44 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import HighlightedTextEditor
let betweenUnderscores = try! NSRegularExpression(pattern: "_[^_]+_", options: [])

struct ContentView: View {
@State private var text: String = "here is _bold, italicized, red text_"

@State private var text: String = ""

private let rules: [HighlightRule] = [
HighlightRule(pattern: betweenUnderscores, formattingRules: [
Expand All @@ -46,9 +47,14 @@ struct ContentView: View {
VStack {
HighlightedTextEditor(text: $text, highlightRules: rules)
// optional modifiers
.autocapitalizationType(.words)
.keyboardType(.numberPad)
.autocorrectionType(.no)
.onCommit { print("commited") }
.onEditingChanged { print("editing changed") }
.onTextChange { print("latest text value", $0) }
.onSelectionChange { print("NSRange of current selection", $0)}
.introspect { editor in
// access underlying UITextView or NSTextView
editor.textView.backgroundColor = .green
}
}
}
}
Expand All @@ -71,38 +77,44 @@ Example of using a preset:
HighlightedTextEditor(text: $text, highlightRules: .markdown)
```

### Regex Presets

I've also added a preset variable, `NSRegularExpression.all`, for easily selecting a whole string.

Example of using it:

```swift
HighlightedTextEditor(text: $text, highlightRules: [
HighlightRule(pattern: .all, formattingRule: TextFormattingRule(key: .underlineStyle, value: NSUnderlineStyle.single.rawValue))
])
```

## API

### HighlightedTextEditor

| Parameter | Type | Optional | Description |
| --- | --- | --- | --- |
| `text` | Binding&lt;String\> | No | Text content of the field |
| `highlightRules` | [HighlightRule] | No | Patterns and formatting for those patterns |
| `onEditingChanged` | () -> Void | Yes | Called when the user begins editing |
| `onCommit` | () -> Void | Yes | Called when the user stops editing |
| `onTextChange` | (String) -> Void | Yes | Called whenever `text` changes |

#### Modifiers (UIKit)

- `.autocapitalizationType(_ type: UITextAutocapitalizationType)`
- `.autocorrectionType(_ type: UITextAutocorrectionType)`
- `.backgroundColor(_ color: UIColor)`
- `.defaultColor(_ color: UIColor)`
- `.defaultFont(_ font: UIFont)`
- `.keyboardType(_ type: UIKeyboardType)`
- `.insertionPointColor(_ color: UIColor)`
- `.multilineTextAlignment(_ alignment: TextAlignment)`

#### Modifiers (AppKit)

- `.allowsDocumentBackgroundColorChange(_ allowsChange: Bool)`
- `.backgroundColor(_ color: NSColor)`
- `.defaultColor(_ color: NSColor)`
- `.defaultFont(_ font: NSFont)`
- `.drawsBackground(_ shouldDraw: Bool)`
- `.insertionPointColor(_ color: NSColor)`
- `.multilineTextAlignment(_ alignment: TextAlignment)`
| Parameter | Type | Description |
| --- | --- | --- |
| `text` | Binding&lt;String\> | Text content of the field |
| `highlightRules` | [HighlightRule] | Patterns and formatting for those patterns |

#### Modifiers

- `.introspect(callback: (_ editor: HighlightedTextEditorInternals) -> Void)`: Allows you the developer to access the underlying UIKit or AppKit objects used by HighlightedTextEditor
- `.onCommit(_ callback: @escaping () -> Void)`: Called when the user stops editing
- `.onEditingChanged(_ callback: @escaping () -> Void)`: Called when the user begins editing
- `.onTextChange(_ callback: @escaping (_ editorContent: String) -> Void)`: Called whenever `text` changes
- `.onSelectionChange(_ callback: @escaping (_ selectedRange: NSRange) -> Void)`
- `.onSelectionChange(_ callback: @escaping (_ selectedRanges: [NSRange]) -> Void)` (AppKit only)

### HighlightedTextEditorInternals

Passed as a parameter to `.introspect()` callbacks. Useful for customizing editor behavior in some way not supported by the HLTE API.

| Property | Type | Description |
| --- | --- | --- |
| `textView` | UITextView or NSTextView | For customizing the UIKit/AppKit text editor |
| `scrollView` | NSScrollView? | For customizing the NSScrollView wrapper. Returns `nil` in UIKit |

### HighlightRule

Expand Down
3 changes: 3 additions & 0 deletions Sources/HighlightedTextEditor/.swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
disabled_rules:
- force_try
- orphaned_doc_comment
Loading

0 comments on commit e7f0db3

Please sign in to comment.