how to change font size and font name of uisegmentedcontrol programmatically on swift?
UI can use control appearance, best place to add it is in app delegate, didFinishLaunchingWithOptions
method, use this if you want to set up the same attribute to every UISegmentedControls in your project just once:
let attr = NSDictionary(object: UIFont(name: "HelveticaNeue-Bold", size: 16.0)!, forKey: NSFontAttributeName)
UISegmentedControl.appearance().setTitleTextAttributes(attr as [NSObject : AnyObject] , forState: .Normal)
But if you are going to set up attributes to just one UISegmentedControl or if you want to change it more often base on some condition use this, UISegmentedControl method:
func setTitleTextAttributes(_ attributes: [NSObject : AnyObject]?,
forState state: UIControlState)
Example:
let attr = NSDictionary(object: UIFont(name: "HelveticaNeue-Bold", size: 16.0)!, forKey: NSFontAttributeName)
seg.setTitleTextAttributes(attr as [NSObject : AnyObject] , forState: .Normal)
Segment Control Font size not changing in swift
try this:
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)
How do I change the font of my UISegmentedControl to the Light styled system font?
To create a system font, instead of the standard UIFont constructor use the class method UIFont.systemFontOfSize(fontSize, weight)
.
For the light font use the UIFontDescriptor constant UIFontWeightLight
.
For reference see UIFont and UIFontDescriptor class references.
Set segmented control font for any width class size
Check device model:
let model = UIDevice.currentDevice().model
if model == "iPad" {
let attr = NSDictionary(object: UIFont(name: "HelveticaNeue-Bold", size: 16.0)!, forKey: NSFontAttributeName as NSCopying)
segmentedControl.setTitleTextAttributes(attr as [NSObject : AnyObject] , for: .normal)
}else{
let attr = NSDictionary(object: UIFont(name: "HelveticaNeue", size: 16.0)!, forKey: NSFontAttributeName as NSCopying)
segmentedControl.setTitleTextAttributes(attr as [NSObject : AnyObject] , for: .normal)
}
The code can be better.
Is it possible to use size classes to adjust the text size for UISegmentedControl objects?
You can use size classes in Interface Builder to substitute a different segmented control with a larger font size when you're running on iPad as opposed to iPhone. But you cannot use size class in Interface Builder just to set a different font size on the same segmented control.
Related Topics
Swift's JSONdecoder with Multiple Date Formats in a JSON String
Prefer Large Titles and Refreshcontrol Not Working Well
Get the Last Character of a String Without Using Array
Swiftui Hide Tabbar in Subview
Get the Size (In Bytes) of an Object on the Heap
Writing Data to an Nsoutputstream in Swift 3
How to Change Font Size and Font Name of Uisegmentedcontrol Programmatically on Swift
Is Swift a Dynamic or Static Language
Swift - Drawing Text with Drawinrect:Withattributes:
Swift Function with Args... Pass to Another Function with Args
Swift Protocol and Return Types on Global Functions
Obj-C Cocoapods + Swift Framework
Exponentiation Operator in Swift
Pass C Function Callback in Swift
"Cannot Assign Value of Type 'String' to Type 'Anyobject'", Swift 3, Xcode 8 Beta 6
Beginner Swift Sprite Kit - Node Collision Detection Help (Skphysicscontact)