【问题标题】:Segment Control Font size not changing in swift段控制字体大小没有迅速改变
【发布时间】:2019-11-25 14:28:23
【问题描述】:

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

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】:

    试试这个:

        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)
    

    【讨论】:

    • 请更新您的答案,说明问题所在以及该答案如何解决问题。仅代码的答案是不受欢迎的。
    • 现在工作正常。 @W.M
    【解决方案2】:

    试试这个:

    let font: [AnyHashable : Any] = [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 10)]
    
    segmentControl.setTitleTextAttributes(font as! [NSAttributedString.Key : Any], for: .normal)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-07
      • 2022-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      相关资源
      最近更新 更多