Reputation: 9196
I have set a custom font for my UISegmentedControl, but it seems to disable the default autoAdjustFontSizeToFitWidth parameter.
Here is the code I apply to set my custom font:
UISegmentedControl *segmentedControl = (UISegmentedControl *)subview;
UIFont *font = [UIFont fontWithName:DEFAULT_FONT size:DEFAULT_FONT_SIZE];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
[segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];
Is there a way to preserve it? Many thanks.
EDIT: I would like to avoid
segmentedControl.apportionsSegmentWidthsByContent = YES;
if possible to always have same width for every segment.
Upvotes: 3
Views: 1173
Reputation: 6012
I just had to do this. You can search through the subviews of the segment control and set the properties.
+ (void) setAdjustsFontForWidth:(UIView *) view {
NSArray * subvies = view.subviews;
for(UIView * view in subvies) {
if([view isKindOfClass:[UILabel class]]) {
UILabel * label = (UILabel *)view;
NSLog(@"label found: %@", label.text);
label.adjustsFontSizeToFitWidth = TRUE;
label.minimumScaleFactor = .1;
} else {
[self setAdjustsFontForWidth:view];
}
}
}
Call it with:
[Utils setAdjustsFontForWidth:mySegmentControl];
Upvotes: 2