简体   繁体   English

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

[英]System font disappear on UIButton attributed title

I have an UIButton and I changed its title to attributed . 我有一个UIButton,我将其标题更改为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: 不幸的是,我找不到系统字体,因为当我将UIButton标题赋予属性时,我再也无法获得此菜单:

好的菜单

Instead I only get this 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? 菜单中的系统字体在哪里?使用UIButton属性标题时如何通过界面生成器添加系统字体?

Please don't suggest thing like "Adding a label with _______ under the UIButton" or "UIImageView with the text". 请不要建议“在UIButton下使用_______添加标签”或“带有文本的UIImageView”之类的建议。

The current iOS system font is "San Francisco" 当前的iOS系统字体为“旧金山”

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

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)
    }
}

From storyboard, changing attributed text back to system font can be frustrating. 从情节提要中,将属性文本更改回系统字体可能会令人沮丧。

Solution 1: redo it 解决方案1:重做

Well, if the attributed text is simple enough, I could advise this: 好吧,如果属性文本足够简单,我可以建议这样做:

  1. switch it to plain text: 将其切换为纯文本:
    状态配置
  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 解决方案2:编辑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 切换到版本编辑器(或使用BBEdit或Sublime Text等外部编辑器打开情节提要)以编辑情节提要的XML源
    在此处输入图片说明
  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. 因此,通常,您将使用带有lorem ipsum的情节提要,并从代码中设置适当的NSAttributedString。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM