diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..2d35203 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: [krzyzanowskim] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8a2461a --- /dev/null +++ b/.gitignore @@ -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/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2e92781 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..551fcc4 --- /dev/null +++ b/Package.swift @@ -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"]) + ] +) diff --git a/README.md b/README.md new file mode 100644 index 0000000..6b0a672 --- /dev/null +++ b/README.md @@ -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) diff --git a/Sources/TextEdit/CarretView.swift b/Sources/TextEdit/CarretView.swift new file mode 100644 index 0000000..8ca1915 --- /dev/null +++ b/Sources/TextEdit/CarretView.swift @@ -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 + } +} diff --git a/Sources/TextEdit/GlyphsView.swift b/Sources/TextEdit/GlyphsView.swift new file mode 100644 index 0000000..5a6689d --- /dev/null +++ b/Sources/TextEdit/GlyphsView.swift @@ -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 + } +} diff --git a/Sources/TextEdit/KeyboardViewController.swift b/Sources/TextEdit/KeyboardViewController.swift new file mode 100644 index 0000000..e56f232 --- /dev/null +++ b/Sources/TextEdit/KeyboardViewController.swift @@ -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, with _: UIPressesEvent?) { + NotificationCenter.default.post(name: UIResponder.pressPressesBegan, object: presses) + } + + override func pressesEnded(_ presses: Set, with _: UIPressesEvent?) { + NotificationCenter.default.post(name: UIResponder.pressPressesEnded, object: presses) + } + + override func pressesChanged(_ presses: Set, with _: UIPressesEvent?) { + print("pressesChanged \(presses)") + } + + override func pressesCancelled(_ presses: Set, with _: UIPressesEvent?) { + print("pressesCancelled \(presses)") + } +} + +struct KeyboardView: UIViewControllerRepresentable { + func makeUIViewController(context _: Context) -> UIViewController { + UIKeyboardViewController() + } + + func updateUIViewController(_: UIViewController, context _: Context) {} +} diff --git a/Sources/TextEdit/TextEdit.swift b/Sources/TextEdit/TextEdit.swift new file mode 100644 index 0000000..b4aa1a4 --- /dev/null +++ b/Sources/TextEdit/TextEdit.swift @@ -0,0 +1,243 @@ +import CoreText +import CoreTextSwift +import SwiftUI + +// TODO: +// - carret position expressed in characterIndex, instead CGPoint +// - update (visually) carret possition as soon as index change +// - insert text at the carret position +// - selection + +public struct TextEdit: View { + private struct DragState: Equatable { + var location: CGPoint = .zero + var lastLineHeight: CGFloat = .zero + + static let zero = DragState() + } + + private struct CarretState: Equatable { + var location: CGPoint = .zero + var height: CGFloat = .zero + } + + @Binding public var text: String + @Binding public var font: CTFont + @Binding public var carretWidth: CGFloat + + // Text frame is calculated in MyPreferenceViewSetter + // then propagated to ancestors. Has to be that way + // because we need width at that point + @State private var textFrame: CTFrame? // cached here + @State private var textFrameBox: CGRect? // cached boundingBoxOfPath + @State private var lineOrigins: [CGPoint] = [] // cached + + @State private var carret = CarretState() + @GestureState private var drag: DragState = .zero + + public init(text: Binding, font: Binding, carretWidth: Binding) { + self._text = text + self._font = font + self._carretWidth = carretWidth + } + + private var attributedString: CFAttributedString { + CFAttributedStringCreate(nil, text as CFString, [NSAttributedString.Key.font.rawValue: font] as CFDictionary)! + } + + private func line(at location: CGPoint) -> (idx: Int, origin: CGPoint, descent: CGFloat, line: CTLine)? { + // calculate line height + guard let textFrame = textFrame, + let textFrameBox = textFrameBox, + lineOrigins.isEmpty == false + else { + return nil + } + + // adjust coordinates + let lineOrigins = self.lineOrigins.map { linePoint -> CGPoint in + CGPoint(x: linePoint.x, y: textFrameBox.maxY - linePoint.y) + } + + // find the line. origins come with height only (for our rect layout) + var prevY: CGFloat = 0 + for (lineIdx, lineOrigin) in lineOrigins.enumerated() { + let line = textFrame.lines()[lineIdx] + let (_, descent, _) = line.typographicBounds() + if location.y > prevY, location.y <= lineOrigin.y + descent { + // lineIdx is the line number we found! + return (idx: lineIdx, origin: lineOrigin, descent: descent, line: line) + } + prevY = lineOrigin.y + descent + } + + return nil + } + + private func lineTypographicHeight(at location: CGPoint) -> CGFloat { + line(at: location)?.line.typographicHeight() ?? .zero + } + + // adjust position to always be "in line" + private func lineAdjustedCarretPosition(at location: CGPoint) -> CGPoint { + guard let line = line(at: location) else { + return location + } + + // find character index at location + let characterIdx = line.line.characterIndex(forPosition: location) + let offsetForCharacter = line.line.offsetForCharacterIndex(characterIdx) + + let (_, descent, _) = line.line.typographicBounds() + return CGPoint(x: offsetForCharacter, y: line.origin.y + descent - line.line.typographicHeight()) + } + + public var body: some View { + KeyboardView().frame(width: 0, height: 0).background(Color.clear) + + GeometryReader { _ in + GlyphsView(self.attributedString, self.textFrame) + .simultaneousGesture( + DragGesture().updating(self.$drag) { value, state, _ in + state = DragState(location: self.lineAdjustedCarretPosition(at: value.location), + lastLineHeight: self.lineTypographicHeight(at: value.location)) + } + ) + .simultaneousGesture( + DragGesture() + .onEnded { _ in + // Here! because GestureState value is still valid (yay) and can be read + // It won't work from each gesture onEnded closure + self.carret.location = self.drag.location + self.carret.height = self.drag.lastLineHeight + } + ) + .background( + MyPreferenceViewSetter( + attributedString: self.attributedString + ) + ) + + // when dragging, use GestureState, then use State + if self.drag == .zero { + CarretView(width: self.$carretWidth) + .frame(height: self.carret.height) // current height should be calculated per line + .offset(x: self.carret.location.x, y: self.carret.location.y) + } else { + CarretView(width: self.$carretWidth) + .frame(height: self.drag.lastLineHeight) // current height should be calculated per line + .offset(x: self.drag.location.x, y: self.drag.location.y) + } + } + .onPreferenceChange(TextPreferenceKey.self) { preferences in + // get frame from/for GlyphsView. + // funny enough it's called before GlyphsView body is called + if let textFrame = preferences.first?.textFrame { + self.textFrame = textFrame + self.textFrameBox = textFrame.path().boundingBoxOfPath + self.lineOrigins = textFrame.lineOrigins() + + // FIXME: not correct + // self.carret = CarretState(location: .zero, height: self.font.ascent() + self.font.descent() + self.font.leading()) + + // UPDATE CARRET HERE! + + // move carret + number of characters + // adjust coordinates + let lineOrigins = self.lineOrigins.map { linePoint -> CGPoint in + CGPoint(x: linePoint.x, y: self.textFrameBox!.maxY - linePoint.y) + } + + // find the line of last character + // FIXME: there's no "before" in empty string + let lastCharacterIndex: String.Index + if self.text.isEmpty { + lastCharacterIndex = self.text.startIndex + } else { + lastCharacterIndex = self.text.index(before: self.text.endIndex) + } + + for (lineIdx, line) in textFrame.lines().enumerated() { + if let lineRange = Range(line.stringRange(), in: self.text), + lineRange.contains(lastCharacterIndex) + { + // get the X offset of the last character + let q = NSRange(lineRange, in: self.text) + let lineOffsetX = line.offsetForCharacterIndex(q.upperBound) + + let (_, descent, _) = line.typographicBounds() + let pos = CGPoint(x: lineOrigins[lineIdx].x + lineOffsetX, + y: lineOrigins[lineIdx].y + descent - line.typographicHeight()) + + // update carret + // TODO: use lense here + self.carret = CarretState(location: pos, height: self.font.ascent() + self.font.descent() + self.font.leading()) + } + } + } + } + .onReceive(NotificationCenter.default.publisher(for: UIResponder.pressPressesBegan), perform: { notification in + guard let presses = notification.object as? Set, let press = presses.first, let key = press.key else { + return + } + + switch key.keyCode { + case .keyboardDeleteOrBackspace: + if !self.text.isEmpty { + self.text = String(self.text.dropLast()) + } + case .keyboardReturnOrEnter, .keyboardReturn: + // something wrong with new line + // https://stackoverflow.com/questions/44683156/linecount-for-attributedstring-from-coretext-is-wrong + // Maybe custom framesetter would help here + self.text += "\n" + default: + self.text += key.characters + } + }) + } +} + +struct MyPreferenceViewSetter: View { + let attributedString: CFAttributedString + + var body: some View { + GeometryReader { geometry in + Rectangle() + .fill(Color.clear) + .preference(key: TextPreferenceKey.self, + value: [TextPreferenceData(rect: geometry.frame(in: .local), + // if you link against the new SDK and want to typeset text with a UTF-16 length longer than 4096, + // you now need to pass in the new option `kCTTypesetterOptionAllowUnboundedLayout` + textFrame: self.attributedString.framesetter().createFrame(geometry.frame(in: .local)), + attributedString: self.attributedString)]) + } + } +} + +struct TextPreferenceData: Equatable { + let rect: CGRect + let textFrame: CTFrame + let attributedString: CFAttributedString +} + +struct TextPreferenceKey: PreferenceKey { + typealias Value = [TextPreferenceData] + + static var defaultValue: [TextPreferenceData] = [] + + static func reduce(value: inout [TextPreferenceData], nextValue: () -> [TextPreferenceData]) { + value.append(contentsOf: nextValue()) + } +} + +// Notes: +// +// http://unicode.org/faq/char_combmark.html +// U+01B5 LATIN CAPITAL LETTER Z WITH STROKE +// U+0327 COMBINING CEDILLA +// U+0308 COMBINING DIAERESIS +// "\u{01B5}\u{0327}\u{0308}" // Ƶ̧̈ <- broken here +// "\u{0061}\u{0328}\u{0301}" // ą́ <- broken in Xcode +// "\u{0105}\u{0301}" // ą́ <- ok +// "Z\u{0308}" diff --git a/TextEditSample/AppDelegate.swift b/TextEditSample/AppDelegate.swift new file mode 100644 index 0000000..9e67e87 --- /dev/null +++ b/TextEditSample/AppDelegate.swift @@ -0,0 +1,31 @@ +// +// AppDelegate.swift +// TextEdit +// +// Created by Marcin Krzyzanowski on 31/05/2020. +// Copyright © 2020 Marcin Krzyzanowski. All rights reserved. +// + +import UIKit + +@UIApplicationMain +class AppDelegate: UIResponder, UIApplicationDelegate { + func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { + // Override point for customization after application launch. + return true + } + + // MARK: UISceneSession Lifecycle + + func application(_: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options _: UIScene.ConnectionOptions) -> UISceneConfiguration { + // Called when a new scene session is being created. + // Use this method to select a configuration to create the new scene with. + return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) + } + + func application(_: UIApplication, didDiscardSceneSessions _: Set) { + // Called when the user discards a scene session. + // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. + // Use this method to release any resources that were specific to the discarded scenes, as they will not return. + } +} diff --git a/TextEditSample/Assets.xcassets/AppIcon.appiconset/Contents.json b/TextEditSample/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..9221b9b --- /dev/null +++ b/TextEditSample/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,98 @@ +{ + "images" : [ + { + "idiom" : "iphone", + "scale" : "2x", + "size" : "20x20" + }, + { + "idiom" : "iphone", + "scale" : "3x", + "size" : "20x20" + }, + { + "idiom" : "iphone", + "scale" : "2x", + "size" : "29x29" + }, + { + "idiom" : "iphone", + "scale" : "3x", + "size" : "29x29" + }, + { + "idiom" : "iphone", + "scale" : "2x", + "size" : "40x40" + }, + { + "idiom" : "iphone", + "scale" : "3x", + "size" : "40x40" + }, + { + "idiom" : "iphone", + "scale" : "2x", + "size" : "60x60" + }, + { + "idiom" : "iphone", + "scale" : "3x", + "size" : "60x60" + }, + { + "idiom" : "ipad", + "scale" : "1x", + "size" : "20x20" + }, + { + "idiom" : "ipad", + "scale" : "2x", + "size" : "20x20" + }, + { + "idiom" : "ipad", + "scale" : "1x", + "size" : "29x29" + }, + { + "idiom" : "ipad", + "scale" : "2x", + "size" : "29x29" + }, + { + "idiom" : "ipad", + "scale" : "1x", + "size" : "40x40" + }, + { + "idiom" : "ipad", + "scale" : "2x", + "size" : "40x40" + }, + { + "idiom" : "ipad", + "scale" : "1x", + "size" : "76x76" + }, + { + "idiom" : "ipad", + "scale" : "2x", + "size" : "76x76" + }, + { + "idiom" : "ipad", + "scale" : "2x", + "size" : "83.5x83.5" + }, + { + "idiom" : "ios-marketing", + "scale" : "1x", + "size" : "1024x1024" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/TextEditSample/Assets.xcassets/Contents.json b/TextEditSample/Assets.xcassets/Contents.json new file mode 100644 index 0000000..73c0059 --- /dev/null +++ b/TextEditSample/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/TextEditSample/Base.lproj/LaunchScreen.storyboard b/TextEditSample/Base.lproj/LaunchScreen.storyboard new file mode 100644 index 0000000..865e932 --- /dev/null +++ b/TextEditSample/Base.lproj/LaunchScreen.storyboard @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/TextEditSample/Info.plist b/TextEditSample/Info.plist new file mode 100644 index 0000000..b75fa78 --- /dev/null +++ b/TextEditSample/Info.plist @@ -0,0 +1,62 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + $(PRODUCT_BUNDLE_PACKAGE_TYPE) + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + LSRequiresIPhoneOS + + UIApplicationSceneManifest + + UIApplicationSupportsMultipleScenes + + UISceneConfigurations + + UIWindowSceneSessionRoleApplication + + + UISceneConfigurationName + Default Configuration + UISceneDelegateClassName + $(PRODUCT_MODULE_NAME).SceneDelegate + + + + + UILaunchStoryboardName + LaunchScreen + UIMainStoryboardFile + LaunchScreen + UIRequiredDeviceCapabilities + + armv7 + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + UISupportedInterfaceOrientations~ipad + + UIInterfaceOrientationPortrait + UIInterfaceOrientationPortraitUpsideDown + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + + diff --git a/TextEditSample/SceneDelegate.swift b/TextEditSample/SceneDelegate.swift new file mode 100644 index 0000000..83db890 --- /dev/null +++ b/TextEditSample/SceneDelegate.swift @@ -0,0 +1,59 @@ +// +// SceneDelegate.swift +// TextEdit +// +// Created by Marcin Krzyzanowski on 31/05/2020. +// Copyright © 2020 Marcin Krzyzanowski. All rights reserved. +// + +import SwiftUI +import UIKit + +class SceneDelegate: UIResponder, UIWindowSceneDelegate { + var window: UIWindow? + + func scene(_ scene: UIScene, willConnectTo _: UISceneSession, options _: UIScene.ConnectionOptions) { + // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. + // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. + // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). + + // Create the SwiftUI view that provides the window contents. + let contentView = TextEditingView() + + // Use a UIHostingController as window root view controller. + if let windowScene = scene as? UIWindowScene { + let window = UIWindow(windowScene: windowScene) + window.rootViewController = UIHostingController(rootView: contentView) + self.window = window + window.makeKeyAndVisible() + } + } + + func sceneDidDisconnect(_: UIScene) { + // Called as the scene is being released by the system. + // This occurs shortly after the scene enters the background, or when its session is discarded. + // Release any resources associated with this scene that can be re-created the next time the scene connects. + // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead). + } + + func sceneDidBecomeActive(_: UIScene) { + // Called when the scene has moved from an inactive state to an active state. + // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive. + } + + func sceneWillResignActive(_: UIScene) { + // Called when the scene will move from an active state to an inactive state. + // This may occur due to temporary interruptions (ex. an incoming phone call). + } + + func sceneWillEnterForeground(_: UIScene) { + // Called as the scene transitions from the background to the foreground. + // Use this method to undo the changes made on entering the background. + } + + func sceneDidEnterBackground(_: UIScene) { + // Called as the scene transitions from the foreground to the background. + // Use this method to save data, release shared resources, and store enough scene-specific state information + // to restore the scene back to its current state. + } +} diff --git a/TextEditSample/TextEdit.entitlements b/TextEditSample/TextEdit.entitlements new file mode 100644 index 0000000..ee95ab7 --- /dev/null +++ b/TextEditSample/TextEdit.entitlements @@ -0,0 +1,10 @@ + + + + + com.apple.security.app-sandbox + + com.apple.security.network.client + + + diff --git a/TextEditSample/TextEditSample.xcodeproj/project.pbxproj b/TextEditSample/TextEditSample.xcodeproj/project.pbxproj new file mode 100644 index 0000000..14f98ef --- /dev/null +++ b/TextEditSample/TextEditSample.xcodeproj/project.pbxproj @@ -0,0 +1,363 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 52; + objects = { + +/* Begin PBXBuildFile section */ + 7575AD4F264055BD007B28DE /* TextEdit in Frameworks */ = {isa = PBXBuildFile; productRef = 7575AD4E264055BD007B28DE /* TextEdit */; }; + 7575AD5126405664007B28DE /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7575AD5026405664007B28DE /* AppDelegate.swift */; }; + 75EDB9A124840616007483FF /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 75EDB99F24840616007483FF /* LaunchScreen.storyboard */; }; + 75F96CD3264054FB00120ED2 /* TextEditingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75F96CD2264054FB00120ED2 /* TextEditingView.swift */; }; + 75F96CD52640550600120ED2 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 75F96CD42640550600120ED2 /* Assets.xcassets */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 7575AD5026405664007B28DE /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; + 7575AD5226405718007B28DE /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 75DC27722640553F001C4252 /* TextEdit */ = {isa = PBXFileReference; lastKnownFileType = folder; name = TextEdit; path = ..; sourceTree = ""; }; + 75EDB99124840614007483FF /* TextEditSample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TextEditSample.app; sourceTree = BUILT_PRODUCTS_DIR; }; + 75EDB9A024840616007483FF /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; + 75F96CD2264054FB00120ED2 /* TextEditingView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TextEditingView.swift; sourceTree = ""; }; + 75F96CD42640550600120ED2 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 75EDB98E24840614007483FF /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 7575AD4F264055BD007B28DE /* TextEdit in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 75EDB98824840614007483FF = { + isa = PBXGroup; + children = ( + 75DC27722640553F001C4252 /* TextEdit */, + 75F96CD42640550600120ED2 /* Assets.xcassets */, + 7575AD5226405718007B28DE /* Info.plist */, + 7575AD5026405664007B28DE /* AppDelegate.swift */, + 75F96CD2264054FB00120ED2 /* TextEditingView.swift */, + 75EDB99F24840616007483FF /* LaunchScreen.storyboard */, + 75EDB99224840614007483FF /* Products */, + 75EDB9B12484087F007483FF /* Frameworks */, + ); + indentWidth = 4; + sourceTree = ""; + tabWidth = 4; + }; + 75EDB99224840614007483FF /* Products */ = { + isa = PBXGroup; + children = ( + 75EDB99124840614007483FF /* TextEditSample.app */, + ); + name = Products; + sourceTree = ""; + }; + 75EDB9B12484087F007483FF /* Frameworks */ = { + isa = PBXGroup; + children = ( + ); + name = Frameworks; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 75EDB99024840614007483FF /* TextEditSample */ = { + isa = PBXNativeTarget; + buildConfigurationList = 75EDB9A524840616007483FF /* Build configuration list for PBXNativeTarget "TextEditSample" */; + buildPhases = ( + 75EDB98D24840614007483FF /* Sources */, + 75EDB98E24840614007483FF /* Frameworks */, + 75EDB98F24840614007483FF /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = TextEditSample; + packageProductDependencies = ( + 7575AD4E264055BD007B28DE /* TextEdit */, + ); + productName = TextEdit; + productReference = 75EDB99124840614007483FF /* TextEditSample.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 75EDB98924840614007483FF /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 1150; + LastUpgradeCheck = 1250; + ORGANIZATIONNAME = "Marcin Krzyzanowski"; + TargetAttributes = { + 75EDB99024840614007483FF = { + CreatedOnToolsVersion = 11.5; + LastSwiftMigration = 1250; + }; + }; + }; + buildConfigurationList = 75EDB98C24840614007483FF /* Build configuration list for PBXProject "TextEditSample" */; + compatibilityVersion = "Xcode 9.3"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 75EDB98824840614007483FF; + packageReferences = ( + ); + productRefGroup = 75EDB99224840614007483FF /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 75EDB99024840614007483FF /* TextEditSample */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 75EDB98F24840614007483FF /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 75F96CD52640550600120ED2 /* Assets.xcassets in Resources */, + 75EDB9A124840616007483FF /* LaunchScreen.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 75EDB98D24840614007483FF /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 7575AD5126405664007B28DE /* AppDelegate.swift in Sources */, + 75F96CD3264054FB00120ED2 /* TextEditingView.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + 75EDB99F24840616007483FF /* LaunchScreen.storyboard */ = { + isa = PBXVariantGroup; + children = ( + 75EDB9A024840616007483FF /* Base */, + ); + name = LaunchScreen.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + 75EDB9A324840616007483FF /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 75EDB9A424840616007483FF /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + 75EDB9A624840616007483FF /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_ENTITLEMENTS = TextEdit/TextEdit.entitlements; + "CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development"; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 67RAULRX93; + "ENABLE_HARDENED_RUNTIME[sdk=macosx*]" = NO; + ENABLE_PREVIEWS = YES; + INFOPLIST_FILE = Info.plist; + "IPHONEOS_DEPLOYMENT_TARGET[sdk=macosx*]" = 14.2; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.krzyzanowskim.TextEdit; + PRODUCT_NAME = "$(TARGET_NAME)"; + SUPPORTS_MACCATALYST = YES; + SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2,6"; + }; + name = Debug; + }; + 75EDB9A724840616007483FF /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_ENTITLEMENTS = TextEdit/TextEdit.entitlements; + "CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development"; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 67RAULRX93; + "ENABLE_HARDENED_RUNTIME[sdk=macosx*]" = NO; + ENABLE_PREVIEWS = YES; + INFOPLIST_FILE = Info.plist; + "IPHONEOS_DEPLOYMENT_TARGET[sdk=macosx*]" = 14.2; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.krzyzanowskim.TextEdit; + PRODUCT_NAME = "$(TARGET_NAME)"; + SUPPORTS_MACCATALYST = YES; + SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2,6"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 75EDB98C24840614007483FF /* Build configuration list for PBXProject "TextEditSample" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 75EDB9A324840616007483FF /* Debug */, + 75EDB9A424840616007483FF /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 75EDB9A524840616007483FF /* Build configuration list for PBXNativeTarget "TextEditSample" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 75EDB9A624840616007483FF /* Debug */, + 75EDB9A724840616007483FF /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + +/* Begin XCSwiftPackageProductDependency section */ + 7575AD4E264055BD007B28DE /* TextEdit */ = { + isa = XCSwiftPackageProductDependency; + productName = TextEdit; + }; +/* End XCSwiftPackageProductDependency section */ + }; + rootObject = 75EDB98924840614007483FF /* Project object */; +} diff --git a/TextEditSample/TextEditSample.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/TextEditSample/TextEditSample.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/TextEditSample/TextEditSample.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/TextEditSample/TextEditSample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/TextEditSample/TextEditSample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/TextEditSample/TextEditSample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/TextEditSample/TextEditingView.swift b/TextEditSample/TextEditingView.swift new file mode 100644 index 0000000..b919b3f --- /dev/null +++ b/TextEditSample/TextEditingView.swift @@ -0,0 +1,17 @@ +import SwiftUI +import TextEdit + +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 + ) + } +} +