-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c2d92b
commit 65e047d
Showing
3 changed files
with
107 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | ||
// } | ||
//} |