Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyzanowskim committed May 3, 2021
0 parents commit 0ed1567
Show file tree
Hide file tree
Showing 20 changed files with 1,232 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: [krzyzanowskim]
93 changes: 93 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## User settings
xcuserdata/

## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
*.xcscmblueprint
*.xccheckout

# SPM
Package.resolved

## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
build/
DerivedData/
*.moved-aside
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3

## Obj-C/Swift specific
*.hmap

## App packaging
*.ipa
*.dSYM.zip
*.dSYM

## Playgrounds
timeline.xctimeline
playground.xcworkspace

# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
# Package.resolved
# *.xcodeproj
#
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
# hence it is not needed unless you have added a package configuration file to your project
# .swiftpm

.build/

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# Pods/
#
# Add this line if you want to avoid checking in source code from the Xcode workspace
# *.xcworkspace

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build/

# Accio dependency management
Dependencies/
.accio/

# fastlane
#
# It is recommended to not store the screenshots in the git repo.
# Instead, use fastlane to re-generate the screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control

fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots/**/*.png
fastlane/test_output

# Code Injection
#
# After new code Injection tools there's a generated folder /iOSInjectionProject
# https://github.com/johnno1962/injectionforxcode

iOSInjectionProject/
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2021, Marcin Krzyzanowski
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 changes: 21 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// swift-tools-version:5.3

import PackageDescription

let package = Package(
name: "TextEdit",
platforms: [.macOS(.v10_15), .iOS(.v14)],
products: [
.library(
name: "TextEdit",
targets: ["TextEdit"]),
],
dependencies: [
.package(url: "https://github.com/krzyzanowskim/CoreTextSwift.git", from: "0.0.1"),
],
targets: [
.target(
name: "TextEdit",
dependencies: ["CoreTextSwift"])
]
)
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

# SwiftUI TextEdit View

A proof-of-concept implementation of editable text component in SwiftUI using CoreText for text layout.

Due to SwiftUI limitations (as of May 2021) it's not possible to handle keystrokes just with SwiftUI. To overcome this limitation, the `UIKeyboardViewController` is responsible for handling keys and forward to SwiftUI codebase.

## Authors

[Marcin Krzyzanowski](http://krzyzanowskim.com)
[@krzyzanowskim](https://twitter.com/krzyzanowskim)


## Screenshots

![App Screenshot](https://via.placeholder.com/468x300?text=App+Screenshot+Here)


## Usage/Examples

```swift
struct TextEditingView: View {
@State private var text = "type here...\n"
@State private var font = UIFont.preferredFont(forTextStyle: .body) as CTFont
@State private var carretWidth = 2.0 as CGFloat

var body: some View {
TextEdit(
text: $text,
font: $font,
carretWidth: $carretWidth
)
}
}
```


## FAQ

#### How?

CoreText + SwiftUI.

#### Why?

For fun and profit.


## Related

Here are some related projects

[CoreTextSwift](https://github.com/krzyzanowskim/CoreTextSwift)
21 changes: 21 additions & 0 deletions Sources/TextEdit/CarretView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import SwiftUI

struct CarretView: View {
@Binding var width: CGFloat

var body: some View {
CarretShape()
.stroke(lineWidth: width)
.frame(width: width)
.foregroundColor(Color.accentColor)
}
}

struct CarretShape: Shape {
func path(in rect: CGRect) -> Path {
var path = Path()
path.move(to: .zero)
path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY))
return path
}
}
51 changes: 51 additions & 0 deletions Sources/TextEdit/GlyphsView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import CoreText
import CoreTextSwift
import SwiftUI

// View because it not a single shape (colors and other things is not a single shape)
struct GlyphsView: View {
var attributedString: CFAttributedString
var textFrame: CTFrame?
private let invertY: CGFloat = -1 // invert for macOS

init(_ attributedString: CFAttributedString, _ textFrame: CTFrame?) {
self.attributedString = attributedString
self.textFrame = textFrame
}

var body: some View {
guard let textFrame = textFrame else {
return Path()
}

var path = Path()
let textFrameBox = textFrame.path().boundingBoxOfPath

// transform to top-left coordinates
let lineOrigins = textFrame.lineOrigins()
.map { linePoint -> CGPoint in
CGPoint(x: linePoint.x, y: textFrameBox.maxY - linePoint.y)
}

// draw all lines
for (i, line) in textFrame.lines().enumerated() {
let lineOrigin = lineOrigins[i]
for glyphRun in line.glyphRuns() {
let font = glyphRun.font
let glyphs = glyphRun.glyphs()
let glyphsPositions = glyphRun.glyphPositions()
for (idx, glyph) in glyphs.enumerated() {
let positionTransform = CGAffineTransform(translationX: glyphsPositions[idx].x, y: (invertY * glyphsPositions[idx].y) + lineOrigin.y)
.scaledBy(x: 1, y: 1 * invertY)

// path is nil for space
if let glyphCGPath = font.path(for: glyph, transform: positionTransform) {
path.addPath(Path(glyphCGPath))
}
}
}
}

return path
}
}
34 changes: 34 additions & 0 deletions Sources/TextEdit/KeyboardViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Foundation
import SwiftUI
import UIKit

extension UIResponder {
static let pressPressesBegan = NSNotification.Name("OMOMUIPressPressesBeganNotification")
static let pressPressesEnded = NSNotification.Name("OMOMUIPressPressesEndedNotification")
}

private class UIKeyboardViewController: UIViewController {
override func pressesBegan(_ presses: Set<UIPress>, with _: UIPressesEvent?) {
NotificationCenter.default.post(name: UIResponder.pressPressesBegan, object: presses)
}

override func pressesEnded(_ presses: Set<UIPress>, with _: UIPressesEvent?) {
NotificationCenter.default.post(name: UIResponder.pressPressesEnded, object: presses)
}

override func pressesChanged(_ presses: Set<UIPress>, with _: UIPressesEvent?) {
print("pressesChanged \(presses)")
}

override func pressesCancelled(_ presses: Set<UIPress>, with _: UIPressesEvent?) {
print("pressesCancelled \(presses)")
}
}

struct KeyboardView: UIViewControllerRepresentable {
func makeUIViewController(context _: Context) -> UIViewController {
UIKeyboardViewController()
}

func updateUIViewController(_: UIViewController, context _: Context) {}
}
Loading

0 comments on commit 0ed1567

Please sign in to comment.