Change font size by using size classes

718 views Asked by At

I am using size classes in my storyboard to create adaptive layout and I have attributed UILabels in it. Now I want to change the font size for iPad of these labels but it seems that size classes for UILabel with attributed text is not available in IB. So the question is how to change the font size of a UILabel with attributed string.

1

There are 1 answers

3
Badal Shah On

As per the Wes said:-

You should take a look at AliSoftware's OHAttributedLabel. It is a subclass of UILabel that draws an NSAttributedString and also provides convenience methods for setting the attributes of an NSAttributedString from UIKit classes.

From the sample provided in the repo:

#import "NSAttributedString+Attributes.h"
#import "OHAttributedLabel.h"

/**(1)** Build the NSAttributedString *******/
NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:@"Hello World!"];
// for those calls we don't specify a range so it affects the whole string
[attrStr setFont:[UIFont systemFontOfSize:12]];
[attrStr setTextColor:[UIColor grayColor]];
// now we only change the color of "Hello"
[attrStr setTextColor:[UIColor redColor] range:NSMakeRange(0,5)];


/**(2)** Affect the NSAttributedString to the OHAttributedLabel *******/
myAttributedLabel.attributedText = attrStr;
// Use the "Justified" alignment
myAttributedLabel.textAlignment = UITextAlignmentJustify;
// "Hello World!" will be displayed in the label, justified, "Hello" in red and " World!" in gray.

Note: In iOS 6+ you can render attributed strings using the attributedText property of UILabel.

please check this question for better understand:-

Iphone/Ipad Nsattributed string

EDIT

You should also check this link to change the size of font. You should modify it according to your requirement. like if you don't need dynamicaly then dont use method.bumpFontSize etc.

Change the font size