Swift UIFont IBInspectable - 可能吗?
Swift UIFont IBInspectable - is it possible?
我有一个 UIView
子类。在这个视图中,我创建了 UIlabel
的实例。
现在我想在情节提要中为这些标签设置字体属性。是否可以为 UIFont
创建一个 IBInspectable
?
我的方法之一是:
@IBInspectable var fontName: UIFont
但是不行。
总结一下:我想为 UIView
获取这个:
希望有人能帮帮我,谢谢! :)
想法
您可以使用 Int 枚举 select 某些字体之一。
详情
xCode8.2.1,Swift3
代码
enum FontType: Int
import UIKit
enum FontType: Int {
case Default = 0, Small, Large
var fount: UIFont {
switch self {
case .Default:
return UIFont.systemFont(ofSize: 17)
case .Small:
return UIFont.systemFont(ofSize: 12)
case .Large:
return UIFont.systemFont(ofSize: 24)
}
}
static func getFont(rawValue: Int) -> UIFont {
if let fontType = FontType(rawValue: rawValue) {
return fontType.fount
}
return FontType.Default.fount
}
}
class View: UIView
import UIKit
@IBDesignable
class View: UIView {
private var label: UILabel!
@IBInspectable var textFont:Int = 0
override func draw(_ rect: CGRect) {
super.draw(rect)
label = UILabel(frame: CGRect(x: 20, y: 20, width: 120, height: 40))
label.text = "Text"
label.textColor = .black
label.font = FontType.getFont(rawValue: textFont)
addSubview(label)
}
}
Main.storyboard
结果
我这样做过
fileprivate var _fontSize:CGFloat = 18
@IBInspectable
var font:CGFloat
{
set
{
_fontSize = newValue
lblPlaceholder.font = UIFont(name: _fontName, size: _fontSize)
}
get
{
return _fontSize
}
}
fileprivate var _fontName:String = "Helvetica"
@IBInspectable
var fontName:String
{
set
{
_fontName = newValue
lblPlaceholder.font = UIFont(name: _fontName, size: _fontSize)
}
get
{
return _fontName
}
}
@IBInspectable var isShowfontName : Bool = false
@IBInspectable var fontFamilyIndex : CGPoint = CGPoint(x: -1, y: -1 )
var leoFontName : String? {
if Int(fontFamilyIndex.x) >= 0 && Int(fontFamilyIndex.x) < UIFont.familyNames.sorted().count {
let fontNames = UIFont.fontNames(forFamilyName: UIFont.familyNames.sorted()[Int(fontFamilyIndex.x)])
if Int(fontFamilyIndex.y) >= 0 && Int(fontFamilyIndex.y) < fontNames.count {
return fontNames[Int(fontFamilyIndex.y)]
}
return fontNames.first ?? nil
}
return nil
}
我有一个 UIView
子类。在这个视图中,我创建了 UIlabel
的实例。
现在我想在情节提要中为这些标签设置字体属性。是否可以为 UIFont
创建一个 IBInspectable
?
我的方法之一是:
@IBInspectable var fontName: UIFont
但是不行。
总结一下:我想为 UIView
获取这个:
希望有人能帮帮我,谢谢! :)
想法
您可以使用 Int 枚举 select 某些字体之一。
详情
xCode8.2.1,Swift3
代码
enum FontType: Int
import UIKit
enum FontType: Int {
case Default = 0, Small, Large
var fount: UIFont {
switch self {
case .Default:
return UIFont.systemFont(ofSize: 17)
case .Small:
return UIFont.systemFont(ofSize: 12)
case .Large:
return UIFont.systemFont(ofSize: 24)
}
}
static func getFont(rawValue: Int) -> UIFont {
if let fontType = FontType(rawValue: rawValue) {
return fontType.fount
}
return FontType.Default.fount
}
}
class View: UIView
import UIKit
@IBDesignable
class View: UIView {
private var label: UILabel!
@IBInspectable var textFont:Int = 0
override func draw(_ rect: CGRect) {
super.draw(rect)
label = UILabel(frame: CGRect(x: 20, y: 20, width: 120, height: 40))
label.text = "Text"
label.textColor = .black
label.font = FontType.getFont(rawValue: textFont)
addSubview(label)
}
}
Main.storyboard
结果
我这样做过
fileprivate var _fontSize:CGFloat = 18
@IBInspectable
var font:CGFloat
{
set
{
_fontSize = newValue
lblPlaceholder.font = UIFont(name: _fontName, size: _fontSize)
}
get
{
return _fontSize
}
}
fileprivate var _fontName:String = "Helvetica"
@IBInspectable
var fontName:String
{
set
{
_fontName = newValue
lblPlaceholder.font = UIFont(name: _fontName, size: _fontSize)
}
get
{
return _fontName
}
}
@IBInspectable var isShowfontName : Bool = false
@IBInspectable var fontFamilyIndex : CGPoint = CGPoint(x: -1, y: -1 )
var leoFontName : String? {
if Int(fontFamilyIndex.x) >= 0 && Int(fontFamilyIndex.x) < UIFont.familyNames.sorted().count {
let fontNames = UIFont.fontNames(forFamilyName: UIFont.familyNames.sorted()[Int(fontFamilyIndex.x)])
if Int(fontFamilyIndex.y) >= 0 && Int(fontFamilyIndex.y) < fontNames.count {
return fontNames[Int(fontFamilyIndex.y)]
}
return fontNames.first ?? nil
}
return nil
}