5

I have a custom UIView class which has a UILabel as a subview. I want to set the font size of the label in the Interface Builder so I made a computed property fontSize that gets and sets the UILabel font's point size.

@IBDesignable class UIMongolLabel: UIView {

    private let view = UILabel()

    let mongolFontName = "ChimeeWhiteMirrored" // font is always the same

    @IBInspectable var fontSize: CGFloat {
        get {
            if let font = view.font {
                return font.pointSize
            } else {
                return 0.0
            }
        }
        set {
            view.font = UIFont(name: mongolFontName, size: newValue)
        }
    }

    // ...
}

This works fine if the user sets a value for the Font Size:

enter image description here

But if the user just adds a new custom view, the Font Size is blank.

enter image description here

The answers here say that it doesn't make sense to have a default for a computed property in Swift, but what if I want a default for the Interface Builder? I would like it to say "17" or something. What should I do?

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • Since the computed value is based on view.font, can't you just set that to what you want the default fontSize to be? – picciano Aug 11 '15 at 00:58
  • 2
    Yes, I will probable end up doing this. The problem is that the user can't see what it is when building things in the storyboard. What I would like is for that value to be displayed in the IB. – Suragch Aug 11 '15 at 01:08

0 Answers0