简体   繁体   中英

How to retrieve the font of a UISegmentedControl's title programmatically?

I need to use a UIFont extension on a UISegmentedControl's title, but in order to do so, I have to get access to the UISegmentedControl's title font programmatically.

How can I do that?

I tried to create an instance of a label, and then assign the UISegmentedControl's title to the text of that label but it's not the proper way to do it.

here's my attempt:

let segmentTitle = segmentedControl.titleForSegmentAtIndex(0)
let label = UILabel()
label.text = segmentTitle

I'm looking for a better way to do that ?

The method you're looking for is -setTitleTextAttributes:forState: .

let attributes: [String: AnyObject] = [ NSFontAttributeName: myFont ]
segmentedControl.setTitleTextAttributes(attributes, forState:.Normal)

For the full list of keys you can use in the attributes dictionary, see the Character Attributes section of the UIKit NSAttributedString reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM