简体   繁体   中英

How do I change the font of a UISegmentedControl in swift without changing the font size?

I want to change the font of UISegmentedControl without changing the font size.

This is my code but, obviously, in this case i change the font size too:

let attr = NSDictionary(object: UIFont(name: "Avenir", size: 12.0)!, forKey: NSFontAttributeName)
segmentedControl.setTitleTextAttributes(attr as [NSObject : AnyObject] , forState: .Normal)

How can I solve this?

Try following code, this is not proper way, but we don't have other option, then we should go via this wat

int fontSize;
for (id segment in [segmentedControl subviews]) 
{
    for (id label in [segment subviews]) 
    {
        if ([label isKindOfClass:[UILabel class]])
        {
          fontSize = label.font.pointSize;
          // Here you got the label, now use the font size from here
          break;
        }
    }   
}

Now you got the fontSize, now you can use this to change the font face

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