Change font size of UISegmentedControl

前端 未结 16 1746
死守一世寂寞
死守一世寂寞 2020-11-28 20:54

Can anyone please tell me how can I change the font type and size of UISegmentedControl?

相关标签:
16条回答
  • 2020-11-28 20:55

    Here is a Swift version of the accepted answer:

    Swift 3:

    let font = UIFont.systemFont(ofSize: 16)
    segmentedControl.setTitleTextAttributes([NSFontAttributeName: font],
                                            for: .normal)
    

    Swift 2.2:

    let font = UIFont.systemFontOfSize(16)
    segmentedControl.setTitleTextAttributes([NSFontAttributeName: font], 
        forState: UIControlState.Normal)
    
    0 讨论(0)
  • 2020-11-28 20:55

    XCode 8.1, Swift 3

    import UIKit
    class ViewController: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
            UISegmentedControl.appearance().setTitleTextAttributes(NSDictionary(objects: [UIFont.systemFont(ofSize: 24.0)], 
            forKeys: [NSFontAttributeName as NSCopying]) as? [AnyHashable : Any], 
            for: UIControlState.normal)
        }
    }
    

    just change the number value (ofSize: 24.0)

    Preview

    0 讨论(0)
  • 2020-11-28 20:56
     UISegmentedControl.appearance().setTitleTextAttributes(NSDictionary(objects: [UIFont.systemFont(ofSize: 16.0)],
                                                                            forKeys: [kCTFontAttributeName as! NSCopying]) as? [AnyHashable : Any],
                                                               for: UIControlState.normal)
    
    0 讨论(0)
  • 2020-11-28 21:03

    Here i have updated for iOS8

    [[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], NSFontAttributeName, nil] forState:UIControlStateNormal];
    
    0 讨论(0)
  • 2020-11-28 21:03

    In swift 5,

    let font = UIFont.systemFont(ofSize: 16)
    UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal)
    
    0 讨论(0)
  • 2020-11-28 21:05

    Use the Appearance API in iOS 5.0+:

    [[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], UITextAttributeFont, nil] forState:UIControlStateNormal];
    

    Links: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIAppearance_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40010906

    http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5

    0 讨论(0)
提交回复
热议问题