Can anyone please tell me how can I change the font type and size of UISegmentedControl
?
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)
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)
UISegmentedControl.appearance().setTitleTextAttributes(NSDictionary(objects: [UIFont.systemFont(ofSize: 16.0)],
forKeys: [kCTFontAttributeName as! NSCopying]) as? [AnyHashable : Any],
for: UIControlState.normal)
Here i have updated for iOS8
[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], NSFontAttributeName, nil] forState:UIControlStateNormal];
In swift 5
,
let font = UIFont.systemFont(ofSize: 16)
UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal)
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