2013-07-10 10 views
68

Sto provando a impostare la dimensione del carattere di un UILabel. Non importa quale valore applico, anche se le dimensioni del testo non sembrano cambiare. Ecco il codice che sto usando.iOS: imposta la dimensione del font di UILabel a livello di codice

[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]]; 

risposta

149

Prova [UIFont systemFontOfSize:36] o [UIFont fontWithName:@"HelveticaNeue" size:36] cioè [[self titleLabel] setFont:[UIFont systemFontOfSize:36]];

+4

arrrg, a pochi secondi troppo tardi, ma è giusto. Il sistema non è fontname ... –

+1

setFont è deprecato in iOS 10.2 – TheJeff

6

Questo codice è perfettamente funzionante per me.

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

Anch'io, [[auto titleLabel] setFont: [UIFont systemFontOfSize: 36]]; no e non so perché – Leon

3

sua perché non c'è famiglia di font con il nome @"System" quindi size:36 saranno anche non funzionare ...

Controllare i font disponibili in Xcode in attributo ispettore e cercano

13

Se siete alla ricerca di codice swift:

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

Objective-C:

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

Swift:

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

cambia solo la dimensione del carattere di un UILabel.

+1

Apparentemente, setFont è deprecato, quindi 'label.font = // Qualunque font .' –

+0

@FabricioPH come se volessi impostare' label.font = // font di font del sistema corrente? ? –

+1

@ChenLiYong Non sono più nello sviluppo di iOS. Forse Googling mostrerà alcuni risultati rilevanti sull'impostazione del font del sistema corrente o sul recupero. https://www.google.com/?#q=ios%20system%20font –

1

Per 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; 

} 
+0

Potrebbe anche essere un problema di appartenenza al target dei caratteri. –

0

In Swift 3.0, è possibile utilizzare questo codice qui sotto:

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 
1

Swift 3,0

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