Skip to content

Commit

Permalink
✨ Add Initials View
Browse files Browse the repository at this point in the history
  • Loading branch information
lovetodream committed Jul 20, 2021
1 parent 9c2d92b commit 65e047d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 13 deletions.
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import PackageDescription

let package = Package(
name: "InitialsUI",
platforms: [.macOS(.v10_15), .iOS(.v13), .watchOS(.v6), .tvOS(.v13)],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
Expand Down
99 changes: 96 additions & 3 deletions Sources/InitialsUI/InitialsUI.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,99 @@
public struct InitialsUI {
public private(set) var text = "Hello, World!"
import SwiftUI

public init() {
public struct InitialsUI<Content: View>: View {
@Binding var text: String

let background: Content

var initials: String {
text.components(separatedBy: .whitespaces)
.filter { !$0.isEmpty }
.reduce("") { partialResult, word in
partialResult + String(word.first!.uppercased())
}
}

init(text: Binding<String>, @ViewBuilder background: @escaping () -> Content) {
self.background = background()
self._text = text
}

public var body: some View {
GeometryReader { g in
ZStack {
background

Text(initials)
.modifier(FitToWidth())
.padding()
}
}
}
}

extension InitialsUI {
init(initials: String, @ViewBuilder background: @escaping () -> Content) {
let text = initials.map { "\($0)" }.joined(separator: " ")

self.init(text: .constant(text), background: background)
}
}

extension InitialsUI where Content == Color {
init(text: Binding<String>) {
self.init(text: text) {
Color.gray
}
}

init(initials: String) {
self.init(initials: initials) {
Color.gray
}
}
}

struct FitToWidth: ViewModifier {
var fraction: CGFloat = 1.0
func body(content: Content) -> some View {
GeometryReader { g in
VStack {
Spacer()
content
.font(.system(size: 1000))
.minimumScaleFactor(0.005)
.lineLimit(1)
.frame(width: g.size.width*self.fraction)
Spacer()
}
}
}
}


// MARK: - Development only

struct InitialsUIWrapper: View {
@State var name = ""

public var body: some View {
VStack {
TextField("Name", text: $name)

InitialsUI(text: $name)
}
}
}

struct InitialsUI_Previews: PreviewProvider {
static var previews: some View {
InitialsUIWrapper()
InitialsUI(text: .constant("Timo Zacherl")) {
RadialGradient(colors: [.gray, .blue], center: .trailing, startRadius: 40, endRadius: 180)
}
InitialsUI(initials: "TZ") {
Color.yellow
}.foregroundColor(.white)
InitialsUI(initials: "TZ")
}
}
20 changes: 10 additions & 10 deletions Tests/InitialsUITests/InitialsUITests.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import XCTest
@testable import InitialsUI

final class InitialsUITests: XCTestCase {
func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.
XCTAssertEqual(InitialsUI().text, "Hello, World!")
}
}
//@testable import InitialsUI
//
//final class InitialsUITests: XCTestCase {
// func testExample() throws {
// // This is an example of a functional test case.
// // Use XCTAssert and related functions to verify your tests produce the correct
// // results.
// XCTAssertEqual(InitialsUI().text, "Hello, World!")
// }
//}

0 comments on commit 65e047d

Please sign in to comment.