3
\$\begingroup\$

Main question

I am setting a font for a UITextView that is a subview in a custom view. I only set it if the user didn't specify the font size in the Interface Builder.

I have the following working code

if self.view.font == nil || self.view.font!.fontName != mongolFontName {
    
        view.font = UIFont(name: mongolFontName, size: defaultFontSize)
    
}

but the if line seems more verbose than it needs to be. Is there a more concise way of getting the same result?

Extra background info

This is a continuation in the same vane of a previous question. After making changes based on advice from the answers there (though still more to do), the code I have now is

import UIKit

@IBDesignable class UIMongolTextView: UIView {

    // ********* Unique to TextView *********
    private let view = UITextView()
    let mongolFontName = "ChimeeWhiteMirrored"
    let defaultFontSize: CGFloat = 17
    
    @IBInspectable var text: String {
        get {
            return view.text
        }
        set {
            view.text = newValue
        }
    }
    
    @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)
        }
    }
    
    func setup() {

        view.backgroundColor = UIColor.clearColor()
        
        // set font if user didn't specify size in IB
        if self.view.font == nil || self.view.font!.fontName != mongolFontName {
            
            view.font = UIFont(name: mongolFontName, size: defaultFontSize)
        }
        
    }
    
    
    
    
    // *******************************************
    // ****** General code for Mongol views ******
    // *******************************************
    
    private var oldWidth: CGFloat = 0
    private var oldHeight: CGFloat = 0
    
    // This method gets called if you create the view in the Interface Builder
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    // This method gets called if you create the view in code
    override init(frame: CGRect){
        super.init(frame: frame)
        self.setup()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.setup()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // layoutSubviews gets called multiple times, only need it once
        if self.frame.height == oldHeight && self.frame.width == oldWidth {
            return
        } else {
            oldWidth = self.frame.width
            oldHeight = self.frame.height
        }
        
        // Remove the old rotation view
        if self.subviews.count > 0 {
            self.subviews[0].removeFromSuperview()
        }
        
        // setup rotationView container
        let rotationView = UIView()
        rotationView.frame = CGRect(origin: CGPointZero, size: CGSize(width: self.bounds.height, height: self.bounds.width))
        self.addSubview(rotationView)
        
        // transform rotationView (so that it covers the same frame as self)
        rotationView.transform = translateRotateFlip()
        
        // add view
        view.frame = rotationView.bounds
        rotationView.addSubview(view)
        
    }
    
    func translateRotateFlip() -> CGAffineTransform {
        
        var transform = CGAffineTransformIdentity
        
        // translate to new center
        transform = CGAffineTransformTranslate(transform, (self.bounds.width / 2)-(self.bounds.height / 2), (self.bounds.height / 2)-(self.bounds.width / 2))
        // rotate counterclockwise around center
        transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2))
        // flip vertically
        transform = CGAffineTransformScale(transform, -1, 1)
        
        return transform
    }

}
\$\endgroup\$
4
\$\begingroup\$

There's a much easier way of doing this.

if self.view.font?.fontName != mongolFontName {
    view.font = UIFont(name: mongolFontName, size: defaultFontSize)
}

We should always prefer optional binding and optional unwrapping (?) to forced (!).

When the value we're optionally unwrapping is nil, we just get nil back. And of course, nil and mongolFontName aren't going to be the same (unless you've set up mongolFontName as nil), so we enter the if's body.

Consider this example from the playground:

enter image description here

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.