系统字体在UIButton属性标题上消失

时间:2018-01-30 14:00:07

标签: ios swift uibutton

我有一个UIButton,我将其标题更改为attributed

现在标题已归属,我需要保留字体 - “系统字体”。

不幸的是我无法找到系统字体,因为当我将UIButton标题归因时,我无法再使用此菜单:

the good menu

相反,我只获得此菜单:

bad menu

在这里,在较大的菜单中,我找不到系统字体。

菜单中的系统字体在哪里或者在使用UIButton属性标题时如何通过界面构建​​器添加系统字体?

请不要建议“在UIButton下添加_______标签”或“带文字的UIImageView”之类的内容。

3 个答案:

答案 0 :(得分:1)

从情节提要中,将属性文本更改回系统字体可能会令人沮丧。

解决方案1:重做

好吧,如果属性文本足够简单,我可以建议这样做:

  1. 将其切换为纯文本:
    state configuration
  2. 将字体设置回系统
  3. 将其切换回属性文本
  4. 重新应用所需的格式

解决方案2:编辑XML

如果属性字符串很复杂,则可能需要仔细考虑:

  1. 切换到版本编辑器(或使用BBEdit或Sublime Text这样的外部编辑器打开故事板)以编辑故事板的XML源
    enter image description here
  2. 找到有罪的字体,例如:

    <font key="NSFont" size="17" name="ComicSansMS"/>
    
  3. 替换为元字体,例如:

    <font key="NSFont" metaFont="system" size="17"/>
    
  4. 返回到标准编辑器


请注意,我很少使用情节提要中的属性字符串,因为它不支持翻译。因此,通常,您将使用带有 lorem ipsum 的情节提要,并通过代码设置足够的NSAttributedString。

答案 1 :(得分:0)

当前的iOS系统字体是&#34;旧金山&#34;

您可以通过以下方式使用它:

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

您也可以通过此页面上的链接下载字体:https://developer.apple.com/ios/human-interface-guidelines/visual-design/typography/

答案 2 :(得分:0)

没有故事板,有一种更简单的方法可以做到这一点。

查看此示例代码:

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)
    }
}
相关问题