Ketan Parmar Ketan Parmar - 3 months ago 28
iOS Question

How to resize text (font) to fit in UISegment of UISegmentedControl?

Is there any way to reduce font size that can be fit in single segment of

UISegmentedControl
?

Have tried many thing something like,

[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] adjustsFontSizeToFitWidth];

[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setMinimumScaleFactor:0.5];


AND

NSArray *arr = segment.subviews; // segment is UISegmentedControl object

for (int i = 0; i < arr.count; i++) {

UIView *aSegment = [arr objectAtIndex:i];

for (UILabel *label in aSegment.subviews) {

if ([label isKindOfClass:[UILabel class]]) {

UILabel *myLabel = (UILabel *)label;

[myLabel setNumberOfLines:0];

label.numberOfLines = 0;
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor = 0.5;
}



}
}


able to set number of lines of label of segment like,

[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setNumberOfLines:0];


Can set single segment size as per content like,

segment.apportionsSegmentWidthsByContent = YES;


but every segment has different size in this case.

I want to keep same size of every segment and want to reduce font size that can be fit in
UISegmentLabel (label)
of
UISegmentedControl
something like
minimumscalefactor
or
minimumfontsize
or
adjustsFontSizeToFitWidth
. These properties is not working for label when contains in
UISegmentedControl
.

If any one can help to achieve this, it will be appreciated!!

Thanks in advance!!

Answer

I found the issue, Actually it was my mistake!!! I was setting numberOfLines,adjustsFontSizeToFitWidth,minimumScaleFactor and TitleTextAttributes toghether. If we set titleTextAttribute then minimumScaleFactor can't work.

Comments