用 Swift 5 创建多色单个字母(字符)
Create multicolor single letter (character) with Swift 5
我需要创建彩色字符串,其中每个字母都应具有多色/彩虹色。请参阅所附图片以便更好地理解。这需要使用 Swift 5+ (UIKit) 编码为 iOS 应用程序的仪表板创建。
在网络上,多色字符串有多种解决方案,但字符串的每个字母仅包含单一颜色,因此它们不符合我的目的。必须感谢任何建议。
multicolored letter/character prototype
步骤是:制作一个CAGradientLayer
,然后:
view.layer.addSublayer(gradient)
view.addSubview(label)
view.mask = label
class LoginViewController: UIViewController {
lazy var gradientBG:GradientBg = {
let bg = GradientBg()
return bg
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
gradientBG.frame = view.frame
view.addSubview(gradientBG)
let label = UILabel(frame: view.bounds)
label.text = "Hello World"
label.font = UIFont.boldSystemFont(ofSize: 30)
label.textAlignment = .center
gradientBG.addSubview(label)
gradientBG.mask = label
}
}
class GradientBg:UIView {
override func layoutSubviews() {
super.layoutSubviews()
// Create a gradient layer
let gradient = CAGradientLayer()
// gradient colors in order which they will visually appear
gradient.colors = [UIColor.red.cgColor, UIColor.blue.cgColor]
// Gradient from left to right
gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
// set the gradient layer to the same size as the view
gradient.frame = bounds
// add the gradient layer to the views layer for rendering
self.layer.addSublayer(gradient)
// Create a label and add it as a subview
}
}
在 Zeeshan Ahmed 和 Shadowrun 的帮助下,我解决了我的问题。我的最终解决方案是,
override func viewDidLoad() {
super.viewDidLoad()
let gradient = CAGradientLayer()
gradient.colors = [UIColor.red.cgColor, UIColor.green.cgColor, UIColor.blue.cgColor]
gradient.startPoint = CGPoint(x: 0, y: 0)
gradient.endPoint = CGPoint(x: 0, y: 1)
// bgView is a custom view with 100X100 sized
gradient.frame = bgView.bounds
bgView.layer.addSublayer(gradient)
let label = UILabel(frame: bgView.bounds)
label.text = "8"
label.font = UIFont.boldSystemFont(ofSize: 90)
label.textAlignment = .center
bgView.addSubview(label)
bgView.mask = label
}
预期的输出应该是,
Gradient single letter
感谢大家。
我需要创建彩色字符串,其中每个字母都应具有多色/彩虹色。请参阅所附图片以便更好地理解。这需要使用 Swift 5+ (UIKit) 编码为 iOS 应用程序的仪表板创建。
在网络上,多色字符串有多种解决方案,但字符串的每个字母仅包含单一颜色,因此它们不符合我的目的。必须感谢任何建议。
multicolored letter/character prototype
步骤是:制作一个CAGradientLayer
,然后:
view.layer.addSublayer(gradient)
view.addSubview(label)
view.mask = label
class LoginViewController: UIViewController {
lazy var gradientBG:GradientBg = {
let bg = GradientBg()
return bg
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
gradientBG.frame = view.frame
view.addSubview(gradientBG)
let label = UILabel(frame: view.bounds)
label.text = "Hello World"
label.font = UIFont.boldSystemFont(ofSize: 30)
label.textAlignment = .center
gradientBG.addSubview(label)
gradientBG.mask = label
}
}
class GradientBg:UIView {
override func layoutSubviews() {
super.layoutSubviews()
// Create a gradient layer
let gradient = CAGradientLayer()
// gradient colors in order which they will visually appear
gradient.colors = [UIColor.red.cgColor, UIColor.blue.cgColor]
// Gradient from left to right
gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
// set the gradient layer to the same size as the view
gradient.frame = bounds
// add the gradient layer to the views layer for rendering
self.layer.addSublayer(gradient)
// Create a label and add it as a subview
}
}
在 Zeeshan Ahmed 和 Shadowrun 的帮助下,我解决了我的问题。我的最终解决方案是,
override func viewDidLoad() {
super.viewDidLoad()
let gradient = CAGradientLayer()
gradient.colors = [UIColor.red.cgColor, UIColor.green.cgColor, UIColor.blue.cgColor]
gradient.startPoint = CGPoint(x: 0, y: 0)
gradient.endPoint = CGPoint(x: 0, y: 1)
// bgView is a custom view with 100X100 sized
gradient.frame = bgView.bounds
bgView.layer.addSublayer(gradient)
let label = UILabel(frame: bgView.bounds)
label.text = "8"
label.font = UIFont.boldSystemFont(ofSize: 90)
label.textAlignment = .center
bgView.addSubview(label)
bgView.mask = label
}
预期的输出应该是, Gradient single letter
感谢大家。