Segment Control字体大小没有快速变化

问题描述 投票:0回答:1

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

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)
swift uisegmentedcontrol
1个回答
0
投票

尝试一下:

    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)
© www.soinside.com 2019 - 2024. All rights reserved.