Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change font size and font name of uisegmentedcontrol programmatically on swift?

How to change font size and font name of uisegmentedcontrol programmatically? I used swift.

Here is my code:

self.mysegmentedControl = UISegmentedControl(items: [         NSLocalizedString("Aaaaaa", comment: ""),         NSLocalizedString("Bbbbbb", comment: ""),         NSLocalizedString("Cccccc", comment: ""),         NSLocalizedString("Dddddd", comment: ""),         NSLocalizedString("Eeeeee", comment: ""),         ]) self.mysegmentedControl.addTarget(self, action: "mysegmentedControlDidChange:", forControlEvents: .ValueChanged) self.mysegmentedControl.selectedSegmentIndex = 0 

regards.

like image 557
user1858725 Avatar asked Jan 14 '15 09:01

user1858725


People also ask

How do I change font size in Swift?

Change Font And Size Of UILabel In StoryboardXIB file, open it in the interface builder. Select the label and then open up the Attribute Inspector (CMD + Option + 5). Select the button on the font box and then you can change your text size or font.

How do I change the font of text in Xcode?

Press xcode->preferences. select fonts and colors. select ANY font in the list and press cmd+a (select all fonts) select font size for all editor fonts at the bottom of the window.

How to set a font size for text view in SwiftUI?

There are two ways to set a font size for Text view in SwiftUI. Fixed font size stays the same regardless of user preference. To set fixed font size, we specified the size we wanted at a time when we created a font. We can set this for both the system font and a custom font. We create a system font with Font.system (size:weight:design:) method.

What are the different types of fonts used in Swift?

Text ( "Simple Swift Guide" ). font (. headline ). bold (). italic () Text ( "Simple Swift Guide" ). font (. callout ). foregroundColor (. blue) In a second, more advanced tutorial, we’re going to learn how to use custom font family in your iOS app.

How to set a different font on UIButton programmatically?

To set a different font on UIButton programmatically you will need to create a new instance of UIFont object. The initializer of UIFont accepts two arguments: name and size. where the “GillSans-Italic” is a font name you want to set.

What is the default font for iOS apps?

The default iOS font is called San Francisco and if you don’t explicitly change it, then all of your text will have the default iOS look. Use one of many standard fonts which are great to keep consistency with default iOS look: Some other options of standard fonts include: title, headline, subheadline, body, callout, caption or footnote.


2 Answers

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) 
like image 160
Greg Avatar answered Oct 07 '22 07:10

Greg


For Swift 4

    let font: [AnyHashable : Any] = [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 17)]     segmentedControl.setTitleTextAttributes(font, for: .normal) 
like image 24
PowerSurge Avatar answered Oct 07 '22 05:10

PowerSurge