1

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.

Narender Tak
  • 241
  • 2
  • 12

1 Answers1

1

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

Community
  • 1
  • 1
Badal Shah
  • 7,541
  • 2
  • 30
  • 65
  • I have set my label as attributed instead of plain and now I want to change its font size....I am not doing any thing to this label with the code. – Narender Tak Jun 12 '15 at 10:58
  • can you upload your attributed label's code portion? – Badal Shah Jun 12 '15 at 11:02
  • As I said earlier I haven't done anything in code related to the label....I am setting the text property of UILabel as attributed instead of plain(default) in storyboard. – Narender Tak Jun 12 '15 at 11:10