簡體   English   中英

系統字體在UIButton屬性標題上消失

[英]System font disappear on UIButton attributed title

我有一個UIButton,我將其標題更改為attributed

現在,標題已歸屬,我需要保留字體-“系統字體”。

不幸的是,我找不到系統字體,因為當我將UIButton標題賦予屬性時,我再也無法獲得此菜單:

好的菜單

相反,我只得到以下菜單:

菜單不好

在這里,在較大的菜單中,我找不到系統字體。

菜單中的系統字體在哪里?使用UIButton屬性標題時如何通過界面生成器添加系統字體?

請不要建議“在UIButton下使用_______添加標簽”或“帶有文本的UIImageView”之類的建議。

當前的iOS系統字體為“舊金山”

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

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

您也可以通過此頁面上的鏈接下載字體: https : //developer.apple.com/ios/human-interface-guidelines/visual-design/typography/

沒有情節提要,有一種更簡單的方法來執行此操作。

簽出此示例代碼:

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

從情節提要中,將屬性文本更改回系統字體可能會令人沮喪。

解決方案1:重做

好吧,如果屬性文本足夠簡單,我可以建議這樣做:

  1. 將其切換為純文本:
    狀態配置
  2. 將字體設置回系統
  3. 切換回屬性文本
  4. 重新應用所需的格式

解決方案2:編輯XML

如果屬性字符串很復雜,則可能需要仔細進行以下操作:

  1. 切換到版本編輯器(或使用BBEdit或Sublime Text等外部編輯器打開情節提要)以編輯情節提要的XML源
    在此處輸入圖片說明
  2. 找到有罪的字體,例如:

     <font key="NSFont" size="17" name="ComicSansMS"/> 
  3. 用元字體替換,例如:

     <font key="NSFont" metaFont="system" size="17"/> 
  4. 返回標准編輯器


請注意,我很少使用情節提要中的屬性字符串,因為它不支持翻譯。 因此,通常,您將使用帶有lorem ipsum的情節提要,並從代碼中設置適當的NSAttributedString。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM