2015-11-23 52 views
0

我想創建一個UIView A這是一箇中心的視圖具有在UIView A中心,並擁有兩個UILabel A & B一個UIView B,但它不是爲中心的UILabels。這是我的代碼創建具有標籤

private func generateView(header: String, below: String) { 
    let size: CGSize = self.view.frame.size 
    let mainView = UIView(frame: CGRectMake(0, 0, size.width, size.height)) 
    mainView.backgroundColor = UIColor.clearColor() 
    let contentHolderView = UIView(frame: CGRectMake(0, 0, 350, 172)) 
    contentHolderView.backgroundColor = UIColor.clearColor() 
    let headerLabel = UILabel(frame: CGRectMake(38, 24, 298, 34)) 
    let belowLabel = UILabel(frame: CGRectMake(14, 66, 344, 83)) 
    belowLabel.numberOfLines = 0 

    headerLabel.text = header 
    belowLabel.text = below 

    headerLabel.textColor = UIColor.blackColor() 
    belowLabel.textColor = UIColor.blackColor() 

    headerLabel.font = UIFont(name: "BrandonGrotesque-Bold", size: 30) 
    belowLabel.font = UIFont(name: "BrandonGrotesque-Light", size: 20) 

    headerLabel.textAlignment = .Center 
    belowLabel.textAlignment = .Center 

    contentHolderView.addSubview(headerLabel) 
    contentHolderView.addSubview(belowLabel) 
    contentHolderView.center = mainView.center 
    mainView.addSubview(contentHolderView) 
    mainView.center = self.view.center 
    self.view.addSubview(mainView) 
} 

而這就是我想要實現,這就是我得到什麼我想要實現

My Achievement

+0

你應該使用約束而不是絕對幀。如果您使用這樣的框架,您的輸出將無法適應不同的設備顯示屏尺寸 – Paulw11

+0

您是否可以在代碼中提供幫助?我正在複製用戶界面以顯示在Carouself ScrollView中@ Paulw11 –

回答

1

,而不是試圖直接編碼幀值,我建議你採用自動佈局。這是一個UIView子類的基礎,它可以做你想做的事(我沒有爲字體打擾,但你可以很容易地添加它) -

import UIKit 

@IBDesignable 
class MyView: UIView { 

    var headerLabel:UILabel! 
    var bodyLabel:UILabel! 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     self.addLabels() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     self.addLabels() 
    } 


    func addLabels() { 

     self.translatesAutoresizingMaskIntoConstraints=false 

     self.headerLabel=UILabel() 
     self.headerLabel.translatesAutoresizingMaskIntoConstraints=false 
     self.headerLabel.textAlignment=NSTextAlignment.Center 

     self.addSubview(self.headerLabel) 

     self.bodyLabel=UILabel() 
     self.bodyLabel.translatesAutoresizingMaskIntoConstraints=false 
     self.bodyLabel.numberOfLines=0 
     self.bodyLabel.textAlignment=NSTextAlignment.Center 

     self.addSubview(self.bodyLabel) 
    } 

    override func updateConstraints() { 
     self.addConstraint(NSLayoutConstraint(item: self.headerLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 24)) 

     self.addConstraint(NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.headerLabel, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0)) 

     self.addConstraint(NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.bodyLabel, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0)) 

     self.addConstraint(NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.bodyLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 24)) 

     self.addConstraint(NSLayoutConstraint(item: self.bodyLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.headerLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 12)) 

     self.addConstraint(NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.LeadingMargin, relatedBy: NSLayoutRelation.Equal, toItem: self.bodyLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0)) 
     self.addConstraint(NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.TrailingMargin, relatedBy: NSLayoutRelation.Equal, toItem: self.bodyLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0)) 

     super.updateConstraints() 
    } 

    override func prepareForInterfaceBuilder() { 
     self.headerLabel.text="Header" 
     self.bodyLabel.text="Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum " 
    } 

} 
+0

我可以動態設置標籤文本嗎?像'let theView = MyView(); theView.headerLabel.text =「Lorem ipsolum」' –

+0

當然。這個類將'headerLabel'和'bodyLabel'作爲屬性公開。只有在特殊的'prepareForInterfaceBuilder'函數中設置靜態文本才能在此視圖子類顯示在IB故事板中時提供測試內容 – Paulw11