2016-04-22 72 views
0

我试图使一组UILabel大胆,并认为它可以使用像CSS类的东西来完成。我认为,虽然唯一的办法就是继承UILabel并且将其添加为自定义类每个标签:如何更改多个UI元素的属性

import UIKit 

class BoldLabel: UILabel { 
    override internal var font: UIFont? { 
     get { 
      return UIFont.boldSystemFontOfSize(16.0) 
     } 
    } 
} 

但这给出了与Objective-C的选择‘字体’吸气剂冲突‘字体’错误“消气对于来自超类“UILabel”的'font'具有相同的Objective-C选择器“。那么有没有办法像这样做,或者以不同的方式轻松制作一堆标签的字体为粗体?

+1

检查此链接http://sunnycyk.com/2014/10/ios-subclass-uilabel-swift/,他们做出的一个子类UILabel和你一样。然后他们调用一个设置方法来定制UILabel;在此设置方法中,您可以执行以下操作:self.font = UIFont.boldSystemFontOfSize(12) 然后,当您将此类分配给您的UILabels时,它们将具有粗体字体。 – AnthonyR

+0

@AnonyRn这正是我所需要的,非常简单。如果你发布一个答案,我可以接受它 – mjr

+0

等一下,我会告诉你一个小例子 – AnthonyR

回答

2
import UIKit 

class BoldLabel: UILabel { 

//This method is call when you affect this class to a UILabel in your STORYBOARD 
required init?(coder aDecoder: NSCoder) { 
    super.init(coder:aDecoder) 
    self.setup() 
} 

//This method is call when you programatically create an instance of this UILabel class 
override init(frame:CGRect) { 
    super.init(frame:frame) 
    self.setup() 
} 

func setup() { 
    //All custom properties of this label class here 
    self.font = UIFont.boldSystemFontOfSize(28) 
} 
} 

结果与UILabels从故事板:

enter image description here