2016-12-24 56 views
1

我無法更改程序創建的UILabel中的文本大小。無法更改以編程方式創建的UILabel的文本大小

這裏有一個截屏:

screen grab

這裏的類定義:

class myView: UIView { 
    let theLabel = UILabel() 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     self.theLabel.text = "Text" 
     self.theLabel.backgroundColor = UIColor.lightGray 
     self.backgroundColor = UIColor.blue 

     self.theLabel.adjustsFontSizeToFitWidth = true 
     self.theLabel.textAlignment = .center 
     self.theLabel.numberOfLines = 1 
     self.theLabel.minimumScaleFactor = 0.1 
     self.theLabel.font = UIFont(name: "System", size:160) 
     self.theLabel.translatesAutoresizingMaskIntoConstraints = false 
     self.addSubview(self.theLabel) 

     let selfAspectConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.width, multiplier: 1.0, constant: 0) 

     let labelWidthConstraint = NSLayoutConstraint(item: self.theLabel, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.width, multiplier: 0.5, constant: 0) 

     let heightConstraint = NSLayoutConstraint(item: self.theLabel, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: self.theLabel, attribute: NSLayoutAttribute.width, multiplier: 0.5, constant: 0) 

     let xConstraint = NSLayoutConstraint(item: self.theLabel, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0) 

     let yConstraint = NSLayoutConstraint(item: self.theLabel, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0) 

     self.addConstraints([selfAspectConstraint,labelWidthConstraint, heightConstraint,xConstraint,yConstraint]) 
    } 
    /* 
    // Only override draw() if you perform custom drawing. 
    // An empty implementation adversely affects performance during animation. 
    override func draw(_ rect: CGRect) { 
     // Drawing code 
    } 
    */ 
} 

我試圖改變字體大小。我已將adjustsFontSizeToFitWidth設置爲true並設置爲minimumScaleFactor。文字大小始終相同。我希望它能夠填滿標籤區域,灰色框。

我如何得到這個工作?

+0

,你如何創建'myView'? – aircraft

+1

我認爲@aircraft所要求的原因是因爲你已經在init(編碼器:)而不是init(幀:)中編碼了所有內容。 IB中的 – dfd

+0

。我添加了一個UIView並將該類設置爲myView –

回答

0

嘗試加入這一行,你設置字體後: self.theLabel.font = theLabel.font.withSize(45)

+0

這是行不通的!謝謝!爲什麼我需要明確設置字體大小?我在分配字體時設置了大小。 –

+0

也許沒有名爲「System」的字體。也請嘗試 –

+2

self.theLabel.font = UIFont.systemFont(ofSize:80) –

0

您的代碼按預期工作,沒有任何變化。 enter image description here enter image description here

CustomView1NoXIB.swift

import UIKit 
class CustomView1NoXIB : UIView { 
    let theLabel = UILabel() 

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




    func initSubviews() { 
    self.theLabel.text = "Hello" 
// self.theLabel.text = "Hello World Big Text Becomes Small" 
    self.theLabel.backgroundColor = UIColor.lightGray 
    self.backgroundColor = UIColor.blue 

    self.theLabel.adjustsFontSizeToFitWidth = true 
    self.theLabel.textAlignment = .center 
    self.theLabel.numberOfLines = 1 
    self.theLabel.minimumScaleFactor = 0.1 
    self.theLabel.font = UIFont(name: "System", size:160) 
    self.theLabel.translatesAutoresizingMaskIntoConstraints = false 
    self.addSubview(self.theLabel) 

    let selfAspectConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.width, multiplier: 1.0, constant: 0) 

    let labelWidthConstraint = NSLayoutConstraint(item: self.theLabel, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.width, multiplier: 0.5, constant: 0) 

    let heightConstraint = NSLayoutConstraint(item: self.theLabel, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: self.theLabel, attribute: NSLayoutAttribute.width, multiplier: 0.5, constant: 0) 

    let xConstraint = NSLayoutConstraint(item: self.theLabel, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0) 

    let yConstraint = NSLayoutConstraint(item: self.theLabel, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0) 

    self.addConstraints([selfAspectConstraint,labelWidthConstraint, heightConstraint,xConstraint,yConstraint]) 

    } 
} 

CustomView1ViewController.swift

import UIKit 
class CustomView1ViewController : UIViewController { 
    @IBOutlet weak var customView1: CustomView1! 
    @IBOutlet weak var customView1NoXIB: CustomView1NoXIB! 

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

感謝, 斯利拉姆

+0

感謝您的回覆Sriram。我試圖讓文字變大,以填補框架。 David Lindsay的答案很有效,但我不知道爲什麼。 –

相關問題