2017-09-10 46 views
0

我有一點麻煩搞清楚如何使UIButton標題透明的,使得它的顏色是上海華盈的漸變背景的顏色。使用面膜,使UIButton的文本透明斯威夫特4

我已經看到了有關呈現的按鈕圖像線程,但它是在Objective-C與舊的CG API,我想知道如果有人能在一個更好的辦法來解決這個問題提出建議。

任何意見將不勝感激!

這是我到目前爲止有:

import UIKit 
import Foundation 

class MainViewController: UIViewController { 

    let headingLabel: UILabel = { 
     let label = UILabel() 
     label.text = "Hello, World" 
     label.font = label.font.withSize(30) 
     label.textColor = .white 
     label.textAlignment = .center 
     label.translatesAutoresizingMaskIntoConstraints = false 
     return label 
    }() 

    let continueButton: UIButton = { 
     let button = UIButton() 
     button.setTitleColor(.black, for: .normal) 
     button.setTitle("Continue", for: .normal) 
     button.layer.cornerRadius = 10 
     button.backgroundColor = .white 
     button.translatesAutoresizingMaskIntoConstraints = false 
     return button 
    }() 

    let gradientLayer: CAGradientLayer = { 
     let layer = CAGradientLayer() 
     layer.colors = [ 
      UIColor(red: 96/255, green: 165/255, blue: 238/255, alpha: 1.0).cgColor, 
      UIColor(red: 233/255, green: 97/255, blue: 99/255, alpha: 1.0).cgColor, 
     ] 
     layer.startPoint = CGPoint(x: 0.0, y: 0.0) 
     layer.endPoint = CGPoint(x: 1.0, y: 1.0) 
     return layer 
    }() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     setupView() 
    } 

    override var preferredStatusBarStyle: UIStatusBarStyle { 
     return .lightContent 
    } 

    override func viewDidAppear(_ animated: Bool) { 
     navigationController?.navigationBar.isHidden = true 
    } 

    func setupView() { 
     // root view 
     gradientLayer.frame = view.frame 
     view.layer.addSublayer(gradientLayer) 
     view.addSubview(headingLabel) 
     view.addSubview(continueButton) 

     // constraints 
     let views: [String: UIView] = [ 
      "headingLabel": headingLabel, 
      "continueButton": continueButton, 
      "superview": view 
     ] 

     var constraints: [NSLayoutConstraint] = [] 

     let verticalHeadingLabelConstraint = NSLayoutConstraint.constraints(withVisualFormat: 
      "V:|-100-[headingLabel(30)]", 
      options: [], 
      metrics: nil, 
      views: views) 
     constraints += verticalHeadingLabelConstraint 

     let horizontalHeadingLabelConstraint = NSLayoutConstraint.constraints(withVisualFormat: 
      "H:|-[headingLabel]-|", 
      options: .alignAllCenterX, 
      metrics: nil, 
      views: views) 
     constraints += horizontalHeadingLabelConstraint 

     let verticalContinueButtonConstraint = NSLayoutConstraint.constraints(withVisualFormat: 
      "V:[continueButton(50)]-100-|", 
      options: [], 
      metrics: nil, 
      views: views) 
     constraints += verticalContinueButtonConstraint 

     let horizontalContinueButtonConstraint = NSLayoutConstraint.constraints(withVisualFormat: 
      "H:|-100-[continueButton]-100-|", 
      options: [], 
      metrics: nil, 
      views: views) 
     constraints += horizontalContinueButtonConstraint 

     view.addConstraints(constraints) 
    } 

} 

current look

回答

-1

如果您使用的是故事板製造商。文字顏色旁邊有一個彩色框。按下它,就會有一個名爲opacity的滑塊。

enter image description here

+0

謝謝你的主動,但它應該很容易從我使用的VFL約束和我不是用故事板缺乏'@ IBDesignable'的告訴。不透明只會使'UIButton'層更爲普遍,這不是這個問題的目標。 –

+1

因爲我的錯誤,我沒有閱讀你的代碼只是描述。製作透明的自定義UIColor,並將其與setTitleColour一起使用。讓透明=的UIColor(紅色:0,綠:0,藍:0,阿爾法:0.0) – Nusonic

+0

難道你不覺得,如果它是那麼容易我就已經做到了?添加一個alpha通道會產生相同的效果,因爲它們是相同的東西。我試圖揭示superviews圖層不是UIButton ... –

0

如果你想這樣做,是更好的,你想想一個面具。
要做到這一點你必須寫文成位圖背景下,把它作爲對純色的面具。
如果找到的東西,可以幫助你here
在以下幾個步驟:

  • 您創建一個圖像內容
  • 您設置白色文本用作作爲掩模
  • 你畫的背景下
  • 您創建蒙版內的文本
  • 您創建一個基於純色
  • 另一個圖像你剪輯他們