Can anyone please tell me how can I change the font type and size of UISegmentedControl
?
C# / Xamarin:
segment.SetTitleTextAttributes(new UITextAttributes {
Font = UIFont.SystemFontOfSize(font_size) }, UIControlState.Normal);
Swift Style:
UISegmentedControl.appearance().setTitleTextAttributes(NSDictionary(objects: [UIFont.systemFontOfSize(14.0)], forKeys: [NSFontAttributeName]), forState: UIControlState.Normal)
Extension for UISegmentedControl
for setting Font Size.
extension UISegmentedControl {
@available(iOS 8.2, *)
func setFontSize(fontSize: CGFloat) {
let normalTextAttributes: [NSObject : AnyObject]!
if #available(iOS 9.0, *) {
normalTextAttributes = [
NSFontAttributeName: UIFont.monospacedDigitSystemFontOfSize(fontSize, weight: UIFontWeightRegular)
]
} else {
normalTextAttributes = [
NSFontAttributeName: UIFont.systemFontOfSize(fontSize, weight: UIFontWeightRegular)
]
}
self.setTitleTextAttributes(normalTextAttributes, forState: .Normal)
}
}
You can get at the actual font for the UILabel by recursively examining each of the views starting with the UISegmentedControl. I don't know if this is the best way to do it, but it works.
@interface tmpSegmentedControlTextViewController : UIViewController {
}
@property (nonatomic, assign) IBOutlet UISegmentedControl * theControl;
@end
@implementation tmpSegmentedControlTextViewController
@synthesize theControl; // UISegmentedControl
- (void)viewDidLoad {
[self printControl:[self theControl]];
[super viewDidLoad];
}
- (void) printControl:(UIView *) view {
NSArray * views = [view subviews];
NSInteger idx,idxMax;
for (idx = 0, idxMax = views.count; idx < idxMax; idx++) {
UIView * thisView = [views objectAtIndex:idx];
UILabel * tmpLabel = (UILabel *) thisView;
if ([tmpLabel respondsToSelector:@selector(text)]) {
NSLog(@"TEXT for view %d: %@",idx,tmpLabel.text);
[tmpLabel setTextColor:[UIColor blackColor]];
}
if (thisView.subviews.count) {
NSLog(@"View has subviews");
[self printControl:thisView];
}
}
}
@end
In the code above I am just setting the text color of the UILabel, but you could grab or set the font property as well I suppose.