Reputation: 1480
I am using this code. How can I add GradientColor to strokeColor ? strokeColor only accepts UIColor. Can I cast GradientColor to UIColor?
I want to do this.:
NSAttributedString.Key.strokeColor: LinearGradient(gradient: Gradient(colors: [Color.red, Color.blue]), startPoint: .leading, endPoint: .trailing)
My Code:
struct StrokeTextLabel: UIViewRepresentable {
@Binding var fontName: String
@Binding var text: String
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: 3.0,
NSAttributedString.Key.strokeColor: UIColor.red,// How can I change Gradient
NSAttributedString.Key.font: UIFont(name: fontName, size:30.0)!
]
)
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) { }
}
Upvotes: 0
Views: 1228
Reputation: 535138
You can't use a gradient as a color; it isn't a color. A common technique in many situations is to use your text as a mask to reveal the gradient behind it. For example, that is an easy way to paint your text with a color-change that flows from top to bottom or from left to right.
A great deal, however, depends upon the exact details of what you want to do with the gradient.
Upvotes: 1