diff --git a/Package.swift b/Package.swift index ae3bbce..87df86a 100644 --- a/Package.swift +++ b/Package.swift @@ -6,7 +6,7 @@ import PackageDescription let package = Package( name: "VText", platforms: [ - .macOS(.v10_15), .iOS(.v13), .tvOS(.v13) + .macOS(.v11), .iOS(.v13), .tvOS(.v13) ], products: [ // Products define the executables and libraries a package produces, and make them visible to other packages. diff --git a/Sources/VText/VText.swift b/Sources/VText/VText.swift index e612b08..eb4f880 100644 --- a/Sources/VText/VText.swift +++ b/Sources/VText/VText.swift @@ -1,66 +1,76 @@ import SwiftUI public struct VText : View { - var text : String + @State var text : String var fontWeight : FontWeight = FontWeight(rawValue: "") ?? .medium var fontSize : CGFloat = 17 var spacing : CGFloat = 6 var alignment : HorizontalAlignment = .leading -// init(text: String) { -// self.text = text // Error: Cannot assign value of type 'String' to type 'State' -// } - - public init(text : String ,fontWeight: FontWeight = FontWeight(rawValue: "") ?? .medium, fontSize: CGFloat = 17, spacing: CGFloat = 6, alignment: HorizontalAlignment = .leading) { - self.text = text + public init(text : State ,fontWeight: FontWeight = FontWeight(rawValue: "") ?? .medium, fontSize: CGFloat = 17, spacing: CGFloat = 6, alignment: HorizontalAlignment = .leading) { + self._text = text self.fontWeight = fontWeight self.fontSize = fontSize self.spacing = spacing self.alignment = alignment } - + + @State private var textIndex : [String] = [] public var body: some View{ VStack(alignment: alignment, spacing: spacing){ - ForEach(0 ..< text.count) { textIndex in + ForEach(Array(zip(textIndex, 0 ..< textIndex.count)), id: \.0) { item in switch fontWeight.rawValue{ case "light" : - Text(String(text[text.index(text.startIndex, offsetBy: textIndex)])) + Text(textIndex[item.1]) .font(.system(size: fontSize)) .fontWeight(.light) case "regular": - Text(String(text[text.index(text.startIndex, offsetBy: textIndex)])) + Text(textIndex[item.1]) .font(.system(size: fontSize)) .fontWeight(.regular) case "medium": - Text(String(text[text.index(text.startIndex, offsetBy: textIndex)])) + Text(textIndex[item.1]) .font(.system(size: fontSize)) .fontWeight(.medium) case "bold": - Text(String(text[text.index(text.startIndex, offsetBy: textIndex)])) + Text(textIndex[item.1]) .font(.system(size: fontSize)) .fontWeight(.bold) case "heavy": - Text(String(text[text.index(text.startIndex, offsetBy: textIndex)])) + Text(textIndex[item.1]) .font(.system(size: fontSize)) .fontWeight(.heavy) case "black": - Text(String(text[text.index(text.startIndex, offsetBy: textIndex)])) + Text(textIndex[item.1]) .font(.system(size: fontSize)) .fontWeight(.black) default: - Text(String(text[text.index(text.startIndex, offsetBy: textIndex)])) + Text(textIndex[item.1]) .font(.system(size: fontSize)) .fontWeight(.regular) } } + Text("\(textIndex.count)") + + } + .onAppear{ + for i in 0 ..< text.count { + textIndex.append(String(text[text.index(text.startIndex, offsetBy: i)])) + } + } + .onChange(of: text) { newValue in + textIndex = [] + for i in 0 ..< text.count { + textIndex.append(String(text[text.index(text.startIndex, offsetBy: i)])) + } } } }