Skip to content

Commit

Permalink
Fixed a problem that occurred when editing text
Browse files Browse the repository at this point in the history
  • Loading branch information
NuPlay committed Apr 17, 2021
1 parent 40f0db9 commit 5afb1e6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
42 changes: 26 additions & 16 deletions Sources/VText/VText.swift
Original file line number Diff line number Diff line change
@@ -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<String>'
// }

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<String> ,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)]))
}
}
}
}
Expand Down

0 comments on commit 5afb1e6

Please sign in to comment.