0
這是我自定義的UILabel代碼:加入的UILabel初始化參數
class RoleLabel: UILabel {
required init?(coder aDecoder: NSCoder) {
super.init(coder:aDecoder)
self.setup()
}
override init(frame:CGRect) {
super.init(frame:frame)
self.setup()
}
override func awakeFromNib() {
super.awakeFromNib()
self.setup()
}
func setup(){
numberOfLines = 1
font = UIFont(name:"HelveticaNeue-Bold", size: 16.0)
sizeToFit()
frame = CGRectMake(frame.minX, frame.minY, frame.width + 40, frame.height+5)
backgroundColor = UIColor.blackColor()
textColor = UIColor.whiteColor()
layer.cornerRadius = 5
layer.shadowOffset = CGSize(width: 2, height: 2)
layer.shadowOpacity = 0.7
layer.shadowRadius = 2
drawTextInRect(frame)
let btn = UIButton()
btn.setTitle("X", forState: .Normal)
btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
btn.frame = CGRectMake(frame.width-20, 1, 20, 23)
addSubview(btn)
}
let topInset = CGFloat(0), bottomInset = CGFloat(0), leftInset = CGFloat(3), rightInset = CGFloat(0)
override func drawTextInRect(rect: CGRect) {
let insets: UIEdgeInsets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
super.drawTextInRect(UIEdgeInsetsInsetRect(rect, insets))
}
}
然而,由於標籤根據文本調整大小,我需要將文本作爲參數傳遞給初始化程序。
有沒有辦法添加這種自定義參數? 它不允許我將它添加到override init(frame:CGRect)
on self.init(frame:frame)它表示:「缺少參數文本的參數」@ tx2 – dpstart
您是否將此初始化程序粘貼到您描述的類上? – tx2
我已經按照你的方式編寫了它,它說:「缺少參數文本的參數」,在行上:'self.init(frame:frame)' – dpstart