Swift iOS:将渐变背景设置为UITextView?

时间:2017-07-05 05:42:42

标签: ios swift xcode uitextview

我试图将渐变背景应用于UITextView对象,不确定它是否可行,因为我得到的是白色背景。

这是我的UIView扩展名:

extension UIView{

func setTextGradient(startColor:UIColor,endColor:UIColor)
{
    let gradient:CAGradientLayer = CAGradientLayer()
    gradient.colors = [startColor.cgColor, endColor.cgColor]
    gradient.locations = [0.0 , 1.0]
    gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
    gradient.endPoint = CGPoint(x: 0.0, y: 1.0)
    gradient.frame = self.bounds
    self.layer.insertSublayer(gradient, at: 0)
}

我在这里为文本视图指定渐变:

        detailTxtView.translatesAutoresizingMaskIntoConstraints = false
    detailTxtView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
    detailTxtView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -8).isActive = true
    detailTxtView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 8).isActive = true
    detailTxtView.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.5).isActive = true

    detailTxtView.setTextGradient(startColor: Constants.Colors.bluLight, endColor: Constants.Colors.silver)

    // Set up text
    let attributedTxt = NSMutableAttributedString(string: titleStr, attributes:[NSFontAttributeName: UIFont(name: "Jazeel-Bold", size: 24)!, NSForegroundColorAttributeName: Constants.Colors.bluLight])

    attributedTxt.append(NSAttributedString(string: "\n\n\(detailStr)", attributes:[NSFontAttributeName: UIFont(name: "HacenTunisia", size: 20)!, NSForegroundColorAttributeName: Constants.Colors.aluminum]))

    let txtStyle = NSMutableParagraphStyle()
    txtStyle.alignment = .center
    let length = attributedTxt.string.count
    attributedTxt.addAttribute(NSParagraphStyleAttributeName, value: txtStyle, range: NSRange(location: 0, length: length))

    detailTxtView.attributedText = attributedTxt

以下是Constants类中的颜色:

    struct Colors{ 
    static let bluDark      = UIColor(colorLiteralRed: 51/255, green: 50/255, blue: 80/255, alpha: 1)
    static let bluLight     = UIColor(colorLiteralRed: 68/255, green: 85/255, blue: 138/255, alpha: 1)
    static let greenLight   = UIColor(colorLiteralRed: 200/255, green: 255/255, blue: 132/255, alpha: 1)
    static let mercury      = UIColor(colorLiteralRed: 235/255, green: 235/255, blue: 235/255, alpha: 1)
    static let silver       = UIColor(colorLiteralRed: 214/255, green: 214/255, blue: 214/255, alpha: 1)
    static let magnesium    = UIColor(colorLiteralRed: 192/255, green: 192/255, blue: 192/255, alpha: 1)
    static let aluminum     = UIColor(colorLiteralRed: 169/255, green: 169/255, blue: 169/255, alpha: 1)
    static let blu2         = UIColor(colorLiteralRed: 88/255, green: 120/255, blue: 166/255, alpha: 0.2)
    static let bluLighter = #colorLiteral(red: 0.451, green: 0.5569, blue: 0.8314, alpha: 1) /* #738ed4 */
}

使用渐变方法时,我总是得到白色背景。感谢任何建议。

4 个答案:

答案 0 :(得分:4)

我认为你忘记了位置

extension UIView
{
   func setGradient(startColor:UIColor,endColor:UIColor)
    {
        let gradient:CAGradientLayer = CAGradientLayer()
        gradient.colors = [startColor.cgColor, endColor.cgColor]
        gradient.locations = [0.0 , 1.0]
        gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
        gradient.endPoint = CGPoint(x: 0.0, y: 1.0)
        gradient.frame = self.bounds
        self.layer.insertSublayer(gradient, at: 0)
     }
}

使用

txtView.setGradient(startColor: UIColor.blue, endColor: UIColor.green)

输出:

enter image description here

答案 1 :(得分:1)

您需要使用locations

CAGradientLayer属性
extension UIView{

    func setGriadientBackrnd(color1: UIColor, color2: UIColor){
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = bounds
        gradientLayer.colors = [color1.cgColor, color2.cgColor]
        gradientLayer.locations = [0.5 , 0.5] //here you can set percentage of color part are display betwin 0 to 1
        layer.insertSublayer(gradientLayer, at: 0)
    }
}

答案 2 :(得分:1)

Please check your code Output

嗨民间尝试希望你帮助,

    let titleStr = "Hi How are you ?"
    let detailStr = "HI How UR preparation..."
    gradientTextview.translatesAutoresizingMaskIntoConstraints = false
    gradientTextview.bottomAnchor.constraint(equalTo:self.view.bottomAnchor).isActive = true
    gradientTextview.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -8).isActive = true
    gradientTextview.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 8).isActive = true
    gradientTextview.heightAnchor.constraint(equalTo: self.view.heightAnchor, multiplier: 0.5).isActive = true

    gradientTextview.setGradient(startColor: UIColor(colorLiteralRed: 68/255, green: 85/255, blue: 138/255, alpha: 1), endColor: UIColor(colorLiteralRed: 214/255, green: 214/255, blue: 214/255, alpha: 1))


    let attributedTxt = NSMutableAttributedString(string: titleStr, attributes:[NSFontAttributeName: UIFont (name: "HelveticaNeue-Bold", size: 20)! , NSForegroundColorAttributeName: UIColor(colorLiteralRed: 68/255, green: 85/255, blue: 138/255, alpha: 1)])

    attributedTxt.append(NSAttributedString(string: "\n\n\(detailStr)", attributes:[NSFontAttributeName: UIFont (name: "HelveticaNeue-Bold", size: 18)! , NSForegroundColorAttributeName:UIColor(colorLiteralRed: 169/255, green: 169/255, blue: 169/255, alpha: 1)]))

    let txtStyle = NSMutableParagraphStyle()
    txtStyle.alignment = .center
    let length = attributedTxt.string.characters.count
    attributedTxt.addAttribute(NSParagraphStyleAttributeName, value: txtStyle, range: NSRange(location: 0, length: length))

    gradientTextview.attributedText = attributedTxt

=============================================== ================

extension UIView{

func setTextGradient(startColor:UIColor,endColor:UIColor)
{
    let gradient:CAGradientLayer = CAGradientLayer()
    gradient.colors = [startColor.cgColor, endColor.cgColor]
    gradient.locations = [0.0 , 1.0]
    gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
    gradient.endPoint = CGPoint(x: 0.0, y: 1.0)
    gradient.frame = self.bounds
    self.layer.insertSublayer(gradient, at: 0)
}

注意: -

而不是查看,您也可以添加self.gradientTextview ...

答案 3 :(得分:1)

在设置文本属性

后设置渐变时,渐变有效
detailTxtView.attributedText = attributedTxt
detailTxtView.setTextGradient(startColor: Constants.Colors.silver, endColor: Constants.Colors.bluDark)