Can anyone please tell me how can I change the font type and size of UISegmentedControl?
Can anyone please tell me how can I change the font type and size of UISegmentedControl?
In swift 5,
let font = UIFont.systemFont(ofSize: 16)
UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal)
UISegmentedControl.appearance().setTitleTextAttributes(NSDictionary(objects: [UIFont.systemFont(ofSize: 16.0)],
forKeys: [kCTFontAttributeName as! NSCopying]) as? [AnyHashable : Any],
for: UIControlState.normal)
Swift 4
let font = UIFont.systemFont(ofSize: 16)
UISegmentedControl.setTitleTextAttributes([NSFontAttributeName: font], for: .normal)
The 12 year old answer (even the edits to it) is broken.
It's just:
let _font = UIFont.systemFont(ofSize: 10)
UISegmentedControl.appearance()
.setTitleTextAttributes([NSAttributedString.Key.font: _font], for: .normal)
It's very likely you will want to change the height correctly while you are at it:
import UIKit
class SmallerSegmentedControl: UISegmentedControl {
override init(frame: CGRect) {
super.init(frame: frame)
common()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
common()
}
func common() {
let _font = UIFont.systemFont(ofSize: 10)
UISegmentedControl.appearance()
.setTitleTextAttributes([NSAttributedString.Key.font: _font], for: .normal)
}
override var intrinsicContentSize:CGSize {
var s = super.intrinsicContentSize
s.height = 24
return s
}
}