88

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]];
LoneWolfPR
  • 3,978
  • 12
  • 48
  • 84

8 Answers8

164

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

JohnVanDijk
  • 3,546
  • 1
  • 24
  • 27
79

Objective-C:

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

Swift:

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

just changes font size of a UILabel.

fujianjin6471
  • 5,168
  • 1
  • 36
  • 32
  • 2
    Apparently, setFont is deprecated, so `label.font = // Whatever font.` – Rudolf Real Apr 24 '15 at 14:55
  • @FabricioPH how if I want to set `label.font = // whatever currently system's font` font ? – Chen Li Yong Jan 11 '16 at 02:33
  • 1
    @ChenLiYong I'm not into iOS development anymore. Maybe Googling will display some relevant results about setting current system's font, or getting it. https://www.google.com/?#q=ios%20system%20font – Rudolf Real Jan 12 '16 at 08:50
25

Swift 3.0 / Swift 4.2 / Swift 5.0

labelName.font = labelName.font.withSize(15)
Community
  • 1
  • 1
Pranit
  • 892
  • 12
  • 20
19

If you are looking for swift code:

var titleLabel = UILabel()
titleLabel.font = UIFont(name: "HelveticaNeue-UltraLight",
                         size: 20.0)
ytbryan
  • 2,644
  • 31
  • 49
7

This code is perfectly working for me.

  UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15,23, 350,22)];
  [label setFont:[UIFont systemFontOfSize:11]];
Code cracker
  • 3,105
  • 6
  • 37
  • 67
Gaurav
  • 553
  • 5
  • 9
  • Me too, [[self titleLabel] setFont:[UIFont systemFontOfSize:36]]; did not and I don't know why – Leon Jul 03 '15 at 15:57
5

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
Imrul Kayes
  • 618
  • 1
  • 10
  • 17
3

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

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Chetan Kumar
  • 290
  • 3
  • 8
1

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;

 }
Ram Vinay Yadav
  • 105
  • 2
  • 8