我要實作的目標是一個單選按鈕
- 一個填充的框架,
- 文本和影像之間的填充
- 以及文本的特定字體。
我的問題是我無法成功組合所有 3 個條件。系統實作了哪些預期屬性取決于特定按鈕的 IB 中 Style 屬性的設定。我也在程式代碼中對填充和字體進行了更改,但體驗到它們被 IB 中的按鈕屬性否決了:
- 當 Style 屬性為“Default”時,可以成功應用所需的字體。(無論是通程序式代碼還是在 IB 中)。但是填充不起作用。
- 當 Style 屬性為“Filled”時,填充和框架引數將起作用,但不應用字體。
這是一個簡化的示例,我希望在其中應用 Courier New 字體。我定義了兩個單選按鈕來演示 Style 屬性的影響。起初是帶有“默認”的版本。我們看到文本是 Courier。但是沒有框架和填充。此外,如果將以編程方式設定填充,則系統會忽略它。

接下來是帶有“已填充”的版本。字體設定被系統忽略。這是我的問題:

有沒有辦法組合框架、字體和填充?任何意見,將不勝感激。作為參考,我也在其中進行了設定的一段代碼。我寫了一個 RadioButton 類,其中應該應用影像、字體和填充。
class RadioButton: UIButton {
// Images
let checkedImage = UIImage(named: "radioButtonSet_25")! as UIImage
let uncheckedImage = UIImage(named: "radioButtonNotSet_25")! as UIImage
// Bool property
var isChecked: Bool = false {
didSet {
self.titleLabel?.font = UIFont(name: "CourierNewPSMT", size:
CGFloat(15))
self.configuration?.imagePadding = (50 as CGFloat)
if isChecked == true {
self.setImage(checkedImage, for: UIControl.State.normal)
} else {
self.setImage(uncheckedImage, for: UIControl.State.normal)
}
}
}
}
uj5u.com熱心網友回復:
我想你可能在故事板和 iOS 15 的基于配置的新按鈕中發現了一個錯誤。你必須在代碼中設定字體。例子
guard var config = button.configuration else {
return
}
config.attributedTitle = try! AttributedString( NSAttributedString(string: "Button", attributes: [
.font: UIFont(name: "Courier New Italic", size: 20)!,
.foregroundColor: UIColor.systemOrange,
]), including: AttributeScopes.UIKitAttributes.self
)
button.configuration = config
結果是:

僅作記錄,這是我的故事板配置:

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/444563.html
