Ali ZahediGol
Ali ZahediGol

Reputation: 1106

how to write custom IBInspectable for fonts and direction?

I decide write extension for text field in swift, and add some feature to UITextField for example add label, title, direction for title, custom color, custom font for title and etc.

The RTL Language direction number (for example phone number) in the Left, but title direction in the Right, because of that I forced separate direction text field and label.

My problem with IBInspectable, because I want add direction and custom font for label text field (title) but IBInspectable don't support this feature of NSTextDirection, UIFont and just support few thinks:

And I don't want use boolean for direction in IBInspectable.

Do you have any idea for this subject?

Thanks for your help, and I apologize for weak English.

Upvotes: 1

Views: 3880

Answers (2)

无夜之星辰
无夜之星辰

Reputation: 6178

UIFont is made up of name and size, so we can use name and size to create UIFont:

@IBInspectable var fontName: String = "HelveticaNeue-Medium" {
    didSet {
        label.font = UIFont.init(name: fontName, size: fontSize)
    }
}

@IBInspectable var fontSize: CGFloat = 12 {
    didSet {
        label.font = UIFont.init(name: fontName, size: fontSize)
    }
}

enter image description here

enter image description here

Upvotes: 1

mikkelam
mikkelam

Reputation: 557

I suppose one way you could hack this is the following:

private var font: UIFont!
@IBInspectable fontStr: String! {
    didSet{
        self.font = UIFont(name: fontStr, size: 15.0)
    }
}

That said, it is probably better not to. This should rather be done via code than with interface builder.

Upvotes: 0

Related Questions