2013-07-10 34 views
68

UILabel'in yazı tipi boyutunu ayarlamaya çalışıyorum. Ne kadar değer kattığım önemli değil ama metin büyüklüğü değişmiyor. İşte kullanıyorum kod.iOS: UILabel Programatik Olarak Yazı Tipi boyutunu ayarlama

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

cevap

149

[UIFont systemFontOfSize:36] veya [UIFont fontWithName:@"HelveticaNeue" size:36] yani [[self titleLabel] setFont:[UIFont systemFontOfSize:36]];

+4

arrrg, çok geç sadece bir kaç saniye, ancak bu doğru. Sistem yazı tipi değil ... –

+1

setFont, iOS 10.2 – TheJeff

6

Bu kod mükemmel benim için çalışıyor deneyin.

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

'dan da kullanımdan kaldırıldı. [[Self titleLabel] setFont: [UIFont systemFontOfSize: 36]]; Ben ve neden bilmiyorum ve neden bilmiyorum – Leon

3

Onun hiçbir yazı @"System" dolayısıyla size:36 da işe yaramaz adıyla aile ...

Kontrol aradığınız özelliğinizde denetiminde xcode mevcuttur ve yazı tipleri

13

deneyin yoktur ÇÜNKÜ swift kodu:

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) 

sadece UILabel yazı tipi boyutunu değiştirir. iOS 8 için

+1

Görünüşe göre, setFont kullanımdan kaldırıldı, yani 'label.font = // Ne olursa olsun font.' –

+0

@FabricioPH' label.font = // şu anda sistemin font 'fontunu ayarlamak istiyorum ? –

+1

@ChenLiYong Artık iOS geliştirmeye girmiyorum. Belki de Googling mevcut sistemin yazı tipini ayarlama veya alma hakkında bazı alakalı sonuçları gösterecektir. https://www.google.com/?#q=ios%20system%20font –

1

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

Ayrıca yazı tipi hedef üyeliğiniz konusunda da sorun olabilir. –

0

Swift 3.0'da, aşağıda bu kodu kullanabilirsiniz:

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) 
İlgili konular