1

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:

  • Int
  • CGFloat
  • Double
  • String
  • Bool
  • CGPoint
  • CGSize
  • CGRect
  • UIColor
  • UIImage

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Ali ZahediGol
  • 876
  • 2
  • 10
  • 20

2 Answers2

1

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

无夜之星辰
  • 5,426
  • 4
  • 25
  • 48
0

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.

Duck
  • 34,902
  • 47
  • 248
  • 470
mikkelam
  • 517
  • 1
  • 7
  • 15