1 Reply
      Latest reply on May 17, 2019 11:28 AM by Claude31
      Claude31 Level 8 Level 8 (6,385 points)

        I observe a strange behavior, not sure I have explored all the context.

         

        I have a titleBar with a button item (backButton IBOutlet).

         

        It is defined in storyboard with a title "< back"

         

        At some point (selection done in tableView), I need to change the font.

         

        If I do:

        1.

                    backButton.title = NSLocalizedString("Validate", comment: "tableView")
                    let boldFont = UIFont.boldSystemFont(ofSize: CGFloat(24)) 
                    backButton.setTitleTextAttributes([
                        NSAttributedString.Key.font : boldFont,
                        NSAttributedString.Key.foregroundColor : UIColor.red,
                        ], for: .normal)

         

         

        I get the exact expected result (string has been localized in a .strings file)

         

        If I just try to keep existing title:

        2.

                     let boldFont = UIFont.boldSystemFont(ofSize: CGFloat(24)) 
                     backButton.setTitleTextAttributes([
                        NSAttributedString.Key.font : boldFont,
                        NSAttributedString.Key.foregroundColor : UIColor.red,
                        ], for: .normal)

         

        Then nothing changes in the button's title

         

        If I set the title with the same string as the original button title

        3.

                    backButton.title = NSLocalizedString("< back", comment: " tableView")
                    let boldFont = UIFont.boldSystemFont(ofSize: CGFloat(24))  
                    backButton.setTitleTextAttributes([
                        NSAttributedString.Key.font : boldFont,
                        NSAttributedString.Key.foregroundColor : UIColor.red,
                        ], for: .normal)

         

        It does not work either: nothing changes

         

        I changed in the stroryboard, replacing "< back" with "<. back"

        Then 2 and 3 work as expected.

         

        It seems that coexistence of the same string for a title non attributed and an attributed one was causing the problem.

        Does that mean it is not possible to change just the font of a UIBarButtonItem ?