我有一个使用NSAttributedString设置给定文本笔画的UIViewRepresentable。
它工作得很好,除了一件事:文本没有填充--只有笔画。
struct StrokedText: UIViewRepresentable {
let text: String
let width: CGFloat
let color: Color
let fontSize: CGFloat
func makeUIView(context: Context) -> UILabel {
let attributedStringParagraphStyle = NSMutableParagraphStyle()
attributedStringParagraphStyle.alignment = NSTextAlignment.center
let attributedString = NSAttributedString(
string: text,
attributes:[
NSAttributedString.Key.paragraphStyle: attributedStringParagraphStyle,
NSAttributedString.Key.strokeWidth: fontSize * 0.05,
NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.strokeColor: UIColor(color),
NSAttributedString.Key.font: UIFont(name:"Coiny-Regular", size: fontSize)!
]
)
let strokeLabel = UILabel(frame: CGRect.zero)
strokeLabel.attributedText = attributedString
strokeLabel.backgroundColor = UIColor.clear
strokeLabel.sizeToFit()
strokeLabel.center = CGPoint.init(x: 0.0, y: 0.0)
return strokeLabel
}
func updateUIView(_ uiView: UILabel, context: Context) {}
}用法:
struct MyView: View {
var body: some View {
GeometryReader { geo in
StrokedText(text: "STORE", width: 0.5, color: Color.black, fontSize: geo.size.width * 0.08)
}
}
}如果.foregroundColor不工作,我如何设置文本的填充颜色?
发布于 2020-11-27 03:15:30
这是因为您正在为文本设置笔触颜色。您需要做的就是为笔划设置一个负值:
.strokeWidth: fontSize * -0.05https://stackoverflow.com/questions/65027874
复制相似问题