iOS: set font size of UILabel Programmatically

IosUilabel

Ios Problem Overview


I'm trying to set the font size of a UILabel. No matter what value I put though the text size doesn't seem to change. Here's the code I'm using.

[self setTitleLabel:[[UILabel alloc] initWithFrame:CGRectMake(320.0,0.0,428.0,50.0)]];
[[self contentView] addSubview:[self titleLabel]];
UIColor *titlebg = [UIColor clearColor];
[[self titleLabel] setBackgroundColor:titlebg];
[[self titleLabel] setTextColor:[UIColor blackColor]];
[[self titleLabel] setFont:[UIFont fontWithName:@"System" size:36]];

Ios Solutions


Solution 1 - Ios

Try [UIFont systemFontOfSize:36] or [UIFont fontWithName:@"HelveticaNeue" size:36] i.e. [[self titleLabel] setFont:[UIFont systemFontOfSize:36]];

Solution 2 - Ios

Objective-C:

[label setFont: [label.font fontWithSize: sizeYouWant]];

Swift:

label.font = label.font.fontWithSize(sizeYouWant)

just changes font size of a UILabel.

Solution 3 - Ios

Swift 3.0 / Swift 4.2 / Swift 5.0

labelName.font = labelName.font.withSize(15)

Solution 4 - Ios

If you are looking for swift code:

var titleLabel = UILabel()
titleLabel.font = UIFont(name: "HelveticaNeue-UltraLight",
                         size: 20.0)

Solution 5 - Ios

This code is perfectly working for me.

  UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15,23, 350,22)];
  [label setFont:[UIFont systemFontOfSize:11]];

Solution 6 - Ios

In Swift 3.0, you could use this code below:

let textLabel = UILabel(frame: CGRect(x:containerView.frame.width/2 - 35, y: 
    containerView.frame.height/2 + 10, width: 70, height: 20))
    textLabel.text = "Add Text"
    textLabel.font = UIFont(name: "Helvetica", size: 15.0) // set fontName and Size
    textLabel.textAlignment = .center
    containerView.addSubview(textLabel) // containerView is a UIView

Solution 7 - Ios

Its BECAUSE there is no font family with name @"System" hence size:36 will also not work ...

Check the fonts available in xcode in attribute inspector and try

Solution 8 - Ios

For iOS 8

  static NSString *_myCustomFontName;

 + (NSString *)myCustomFontName:(NSString*)fontName
  {
 if ( !_myCustomFontName )
  {
   NSArray *arr = [UIFont fontNamesForFamilyName:fontName];
    // I know I only have one font in this family
    if ( [arr count] > 0 )
       _myCustomFontName = arr[0];
  }

 return _myCustomFontName;

 }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLoneWolfPRView Question on Stackoverflow
Solution 1 - IosJohnVanDijkView Answer on Stackoverflow
Solution 2 - Iosfujianjin6471View Answer on Stackoverflow
Solution 3 - IosPranitView Answer on Stackoverflow
Solution 4 - IosytbryanView Answer on Stackoverflow
Solution 5 - IosGauravView Answer on Stackoverflow
Solution 6 - IosImrul KayesView Answer on Stackoverflow
Solution 7 - IosChetan KumarView Answer on Stackoverflow
Solution 8 - Iosuser3622576View Answer on Stackoverflow