Ios Can';t更改以编程方式创建的UILabel中的文本大小
我无法更改以编程方式创建的Ios Can';t更改以编程方式创建的UILabel中的文本大小,ios,swift,text,uilabel,size,Ios,Swift,Text,Uilabel,Size,我无法更改以编程方式创建的UILabel中文本的大小 以下是屏幕抓图: 下面是类定义: class myView: UIView { let theLabel = UILabel() required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.theLabel.text = "Text" self.theLabel.backgroundCo
UILabel
中文本的大小
以下是屏幕抓图:
下面是类定义:
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
设置为true。文本大小始终相同。我希望它能填充标签区域,即灰色框
如何使其工作?设置字体后,尝试添加此行:
self.theLabel.font=theLabel.font.with size(45)您的代码按预期工作,没有任何更改。 CustomView1NoXIB.swift CustomView1ViewController.swift 谢谢,
Sriram,如何创建
myView
?我认为@aircraft询问的原因是因为您已经在init(coder:)中对所有内容进行了编码,而不是在init(frame:)中。在IB中,我添加了一个UIView并将类设置为myView尝试将代码移动到viewDidLoad方法。它在这里工作正常吗?@Avt,他向我们展示的唯一代码是UIView子类,它没有viewDidLoad,但它的视图控制器有。这确实有效!谢谢为什么我需要显式设置字体大小?我在指定字体时设置了大小。可能没有名为“系统”的字体。还有tryself.theLabel.font=UIFont.systemFont(of size:80)这很愚蠢,而且完全是胡扯——但这是iOS 10允许我在从代码创建UILabel
实例时使用微小字体(例如9
)的唯一方法!发疯了……谢谢你的回复。我试图把文本放大,以填充框架。大卫·林赛的答案是有效的,尽管我不知道为什么。
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])
}
}
import UIKit
class CustomView1ViewController : UIViewController {
@IBOutlet weak var customView1: CustomView1!
@IBOutlet weak var customView1NoXIB: CustomView1NoXIB!
override func viewDidLoad() {
super.viewDidLoad()
}
}