Chris Byatt
Chris Byatt

Reputation: 3819

Subclass UITextField to override font when using size classes with custom font

I'm trying to subclass UITextField in order to set the font correctly as per the answer to this question: Custom Font Sizing in XCode6 Size Classes Not Working Properly w/ Custom Fonts

I have a text field with a custom font and when I use size classes I lose ALL custom fonts in my app. They default to system.

I've tried to do the same thing for the UITextField as the answer to that question suggested for UILabel, but with no joy.

My Subclass:

import Foundation
import UIKit

class CustomUITextField: UITextField {

    override func layoutSubviews() {
        super.layoutSubviews()
        self.font = UIFont(name: "Score Board", size: self.font!.pointSize)
    }

}

Upvotes: 0

Views: 641

Answers (1)

Chris Byatt
Chris Byatt

Reputation: 3819

Well this turned out to be a really weird bug. It does work provided there is an element on your storyboard that is not set to System font, but is set to your custom font.

My fix was to put in an invisible label that was not using my subclass but set to the custom font in Interface Builder. Now all my other fonts, which are set to System in Interface Builder, are overridden in my custom classes correctly.

Thanks, Apple!

Upvotes: 1

Related Questions