How to change the font size in iOS 7 if we use a system font?

Size not applicable.

What if I want the font to be larger or smaller?

What should I do?

enter image description here

I thought it depends on the size of the UILabel. However, resizing UILabel will not work.

+4
source share
4 answers

iOS 7 . , , , , , , , . , , , "// ". , . , . , . , , . UIFontDescriptor , fontWithDescriptor:size: , .

@interface SomeViewController ()

@property (weak, nonatomic) IBOutlet UILabel *headlineLabel;

@end

@implementation SomeViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    // We need to setup our fonts whenever controller appears.
    // to account for changes that happened while
    // controller wasn't on screen.
    [self setupFonts];

    // Subscribing to UIContentSizeCategoryDidChangeNotification
    // to get notified when user chages the preferred size.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(preferredFontChanged:)
                                                 name:UIContentSizeCategoryDidChangeNotification
                                               object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillAppear:animated];

    // Unsubscribe from UIContentSizeCategoryDidChangeNotification.
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIContentSizeCategoryDidChangeNotification
                                                  object:nil];
}

- (void)setupFonts
{
    // In this method we setup fonts for all the labels and text views
    // by calling 'preferredFontForTextStyle:scale:'.
    self.headlineLabel.font = [self preferredFontForTextStyle:UIFontTextStyleHeadline scale:0.8];
}

- (UIFont *)preferredFontForTextStyle:(NSString *)style scale:(CGFloat)scale
{
    // We first get prefered font descriptor for provided style.
    UIFontDescriptor *currentDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:style];

    // Then we get the default size from the descriptor.
    // This size can change between iOS releases.
    // and should never be hard-codded.
    CGFloat headlineSize = [currentDescriptor pointSize];

    // We are calculating new size using the provided scale.
    CGFloat scaledHeadlineSize = lrint(headlineSize * scale);

    // This method will return a font which matches the given descriptor
    // (keeping all the attributes like 'bold' etc. from currentDescriptor),
    // but it will use provided size if it greater than 0.0.
    return [UIFont fontWithDescriptor:currentDescriptor size:scaledHeadlineSize];
}

- (void)preferredFontChanged:(NSNotification *)notification
{
    [self setupFonts];
}

@end
+5

, ,

label.font = [UIFont fontWithName:@"HelveticaNeue-Light"  size:20 ];
             OR     
label.font = [UIFont systemFontOfSize:20];

ios system fonts .

+2

, " ".

UILabel .xib. , . , .

, :

float fontSize = (whatever you want the size to be);
myUILabel.font = [UIFont systemFontOfSize:fontSize];
0

:

label.font = [[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline] fontWithSize:label.font.pointSize * scale];

scale , .

0

All Articles