Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
itstheceo committed Apr 4, 2024
0 parents commit 6fc593c
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Colin O'Brien

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 22 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// swift-tools-version: 5.7
import PackageDescription

let package = Package(
name: "TypewriterCarouselView",
platforms: [
.iOS(.v16)
],
products: [
.library(
name: "TypewriterCarouselView",
targets: ["TypewriterCarouselView"]
)
],
dependencies: [],
targets: [
.target(
name: "TypewriterCarouselView",
dependencies: []
)
]
)
40 changes: 40 additions & 0 deletions Sources/TypewriterCarouselView/TypewriterCarouselView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// TypewriterView.swift
//
//
// Created by Colin O'Brien on 4/04/24.
//

import SwiftUI

struct TypewriterCarouselView: View {

@State var text: [String]
@State private var currentText: String?
@State private var cursor: Int = -1

var typingDelay: Duration = .milliseconds(50)
var onWritingFinishedDelay: Duration = .seconds(5)
var onDeletingFinishedDelay: Duration = .seconds(1)
var mode: TypewriterView.Mode = .writeAndDelete

var body: some View {
TypewriterView(
text: currentText,
typingDelay: typingDelay,
onTypingFinished: {
cursor += 1
currentText = text[cursor % text.count]
},
onWritingFinishedDelay: onWritingFinishedDelay,
onDeletingFinishedDelay: onDeletingFinishedDelay,
mode: mode
)
.font(.title)
.padding()
}
}

#Preview {
TypewriterCarouselView(text: ["Hello", "World!"])
}
94 changes: 94 additions & 0 deletions Sources/TypewriterCarouselView/TypewriterView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//
// TypewriterView.swift
//
//
// Created by Colin O'Brien on 4/04/24.
//

import SwiftUI

struct TypewriterView: View {

enum Mode {
case write, writeAndDelete
}

var text: String?
var typingDelay: Duration = .milliseconds(50)
var onTypingFinished: (() -> Void) = {}
var onWritingFinishedDelay: Duration = .seconds(5)
var onDeletingFinishedDelay: Duration = .seconds(1)
var mode: Mode = .writeAndDelete

@State private var animatedText: AttributedString = ""
@State private var typingTask: Task<Void, Error>?

var body: some View {
Text(animatedText)
.onChange(of: text, perform: { _ in animateText() })
.onAppear { animateText() }
}

private func animateText() {
typingTask?.cancel()

guard let text = text else {
onTypingFinished()
return
}

typingTask = Task {
let defaultAttributes = AttributeContainer()
animatedText = AttributedString(
text,
attributes: defaultAttributes.foregroundColor(.clear)
)

var index = animatedText.startIndex
while index < animatedText.endIndex {
try Task.checkCancellation()

// Update the style
animatedText[animatedText.startIndex...index]
.setAttributes(defaultAttributes)

// Wait
try await Task.sleep(for: typingDelay)

// Advance the index, character by character
index = animatedText.index(afterCharacter: index)
}

// Wait
try await Task.sleep(for: onWritingFinishedDelay)

switch mode {
case .write: onTypingFinished()

case .writeAndDelete:
index = animatedText.endIndex

while index > animatedText.startIndex {
try Task.checkCancellation()

animatedText[index..<animatedText.endIndex]
.setAttributes(defaultAttributes.foregroundColor(.clear))

try await Task.sleep(for: typingDelay)

index = animatedText.index(beforeCharacter: index)
}

animatedText.setAttributes(defaultAttributes.foregroundColor(.clear))

try await Task.sleep(for: onDeletingFinishedDelay)

onTypingFinished()
}
}
}
}

#Preview {
TypewriterView(text: "Hello World!")
}

0 comments on commit 6fc593c

Please sign in to comment.