2
votes

I have an UIButton and I changed its title to attributed.

Now that the title is attributed I need to keep the font - "System font".

Unfortunately I can't find system font because when I made the UIButton title to attributed, I can't get this menu anymore:

the good menu

Instead I only get this menu:

bad menu

and here, in the larger menu, I can't find system font.

where is the system font in the menu or how can I add system font via interface builder when using UIButton attributed title?

Please don't suggest thing like "Adding a label with _______ under the UIButton" or "UIImageView with the text".

3
Family is system font only in that pic. scroll it, select your font, you will get typeface also as regular and all. - Sharad Chauhan

3 Answers

1
votes

From storyboard, changing attributed text back to system font can be frustrating.

Solution 1: redo it

Well, if the attributed text is simple enough, I could advise this:

  1. switch it to plain text:
    state configuration
  2. set the font back to system
  3. switch it back to attributed text
  4. re-apply the desired formatting

Solution 2: edit the XML

If the attributed string is complex, you may want to go carefully:

  1. switch to the Version Editor (or open the storyboard with an external editor like BBEdit or Sublime Text) to edit the XML source of the storyboard
    enter image description here
  2. find the guilty font, like:

    <font key="NSFont" size="17" name="ComicSansMS"/>
    
  3. replace with a meta font, like:

    <font key="NSFont" metaFont="system" size="17"/>
    
  4. return to the Standard Editor


Note that I rarely use attributed strings from storyboard because it can't support translations. So in general, you'll use the storyboard with some lorem ipsum, and set the adequate NSAttributedString from code.

0
votes

The current iOS system font is "San Francisco"

You can use it via:

UIFont.systemFont(ofSize: CGFloat, weight: UIFontWeight)

You can also download the fonts via a link on this page: https://developer.apple.com/ios/human-interface-guidelines/visual-design/typography/

0
votes

There is a simpler way to do this without the storyboard.

Checkout this sample code:

class VC: UIViewController{

    @IBOutlet weak var button: UIButton! //button outlet

    override func viewDidLoad() {
        super.viewDidLoad()
        // Initialization code
        ///here we are defining the attributes for the button title...

        let textAttribute: [NSAttributedStringKey : Any] = [.foregroundColor: UIColor.blue, .font: UIFont.systemFont(ofSize: 14)]

        self.button.setAttributedTitle(NSAttributedString(string: "Button", attributes: textAttribute), for: .selected)
    }
}