段控件字体大小在 swift 中未更改

Segment Control Font size not changing in swift

我有一个分段控件,我正在尝试更改分段标题的字体大小。我尝试了多个代码,但它不工作。这就是我如何更改分段控件的字体大小:

let font = UIFont.systemFont(ofSize: 16)
segmentControl.setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal)

但是当我 运行 应用程序时,它并没有改变字体大小。 这是我在 viewDidLoad:

中的所有代码
let font = UIFont.systemFont(ofSize: 16)
segmentControl.setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal)
let titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
segmentControl.setTitleTextAttributes(titleTextAttributes, for: .selected)
segmentControl.fallBackToPreIOS13Layout(using: UIColor.clear)
segmentControl.font(name: "Helvetica", size: 16)
let titleTextAttributesForSelected = [NSAttributedString.Key.foregroundColor: UIColor.white]
let titleTextAttributesForNormal = [NSAttributedString.Key.foregroundColor: UIColor.gray]
segmentControl.setTitleTextAttributes(titleTextAttributesForSelected, for: .selected)
segmentControl.setTitleTextAttributes(titleTextAttributesForNormal, for: .normal)

试试这个:

    let font = UIFont.systemFont(ofSize: 16)

    let normalAttribute: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: UIColor.gray]
    segmentControl.setTitleTextAttributes(normalAttribute, for: .normal)

    let selectedAttribute: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: UIColor.red]
    segmentControl.setTitleTextAttributes(selectedAttribute, for: .selected)

试试这个:

let font: [AnyHashable : Any] = [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 10)]

segmentControl.setTitleTextAttributes(font as! [NSAttributedString.Key : Any], for: .normal)