2009-08-19 8 views
28

내 응용 프로그램에 다음 코드가 있습니다.UILabel - iPhone에서 프로그래밍 방식으로 글꼴 - 서체 설정

tmp=[[UILabel alloc] initWithFrame:label1Frame]; 
tmp.tag=1; 
tmp.textColor=[UIColor blackColor]; 
[tmp setFont:[UIFont fontWithName:@"American Typewriter" size:18]]; 
tmp.backgroundColor=[UIColor clearColor]; 
[cell.contentView addSubview:tmp]; 
[tmp release]; 

지금, 나는

======> 어떻게 코드를 통해 "서체 = 굵게"로 설정 알 필요가?

+0

"레이블의 그림자 오프셋"이란 레이블의 그림자의 크기를 의미합니까? – devinfoley

+0

@flashcards - 해결책을 찾아 섀도우를 찾을 필요가 없습니다. [tmp setshadowoffset : cgsizemake (1,1)]; –

+2

@sagar 당신은 자신의 질문에 대한 답변으로 게시하여 저장하게해야합니다. 건배! – devinfoley

답변

36

가족 내에서 bold 글꼴의 이름을 사용해야합니다. 미국 타자기의 bold 버전이 있는지 확인하려면 콘솔에

[UIFont fontNamesForFamilyName:@"AmericanTypewriter"] 

를 출력하려고합니다.

이 경우 "AmericanTypewriter-Bold"을 사용해야합니다.

[UIFont fontNamesForFamilyName:@"AmericanTypewriter-Bold"] 
+0

@flashcards - 아니, 논리를 시험해 보았지만 응용 프로그램이 고장났습니다. –

+0

Hmmm ... fontNamesForFamilyName은 패밀리 이름을 취하는 UIFont의 정적 메서드입니다. 나는 지금 내 대답을 고칠 것이다. "AmericanTypewriter-Bold"를 사용해 보셨습니까? – devinfoley

+5

@sagar - "American Typewriter"사이에는 공백이 없습니다. 나는 그들 사이에 공간을 두었다고 생각합니다. – Brij

20

무엇 setter 속성을 사용하는 방법에 대한 :

// Create a string 
NSString *text = @"You are getting sleepy."; 

// Get a font to draw it in 
UIFont *font = [UIFont boldSystemFontOfSize:28]; 

// Draw the string 
[text drawInRect:(CGRect) 
withFont:font]; 
+0

시스템 글꼴을 사용하려는 경우에만 유용합니다. @Spark는 시스템 글꼴을 사용하지 않았습니다. –

17

그냥 NSLog 당신이 얻을 원하는 글꼴 :

NSLog(@" %@", [UIFont fontNamesForFamilyName:@"American Typewriter"]); 

을 그리고 당신은 배열 얻을 :

(
    "AmericanTypewriter-CondensedLight", 
    "AmericanTypewriter-Light", 
    "AmericanTypewriter-Bold", 
    AmericanTypewriter, 
    "AmericanTypewriter-CondensedBold", 
    "AmericanTypewriter-Condensed" 
) 

사용 당신이 필요로하는 코드 :

@devinfoley 쓴처럼
UILabel * loading = [[UILabel alloc] initWithFrame:CGRectMake(350, 402, 150, 25)]; 
    [loading setFont:[UIFont fontWithName:@"AmericanTypewriter-Bold" size:12.0]]; 
    [loading setTextColor:[UIColor whiteColor]]; 
    [loading setBackgroundColor:[UIColor clearColor]]; 
    [loading setText:@"Loading ..."]; 
    [self.view addSubview:loading]; 
1

, 당신은 fontName 사용중인에 -Bold을 추가해야합니다. 나를 위해 fontNamesForFamilyName 함께 작동하지 않는 대신 fontWithName:size 사용하고 있습니다. 프로그래밍 방식으로 UILabel을 만드는 경우 작동 할 수 있습니다. 제 경우에는 font을 내 UILabel에 내고 awakeFromNib에 을 에 넣고이 확장자를 class으로 설정하십시오. self.font.pointSize을 사용하면 Interface Builder 안에 fontSize을 설정할 수 있으며 더 많은 UI 요소가 있으면 더 잘 처리 할 수 ​​있습니다.

- (void)awakeFromNib{ 

    [super awakeFromNib]; 
    [self setAdjustsFontSizeToFitWidth:YES]; 
    [self setFont:[UIFont fontWithName:@"Font-Bold" size:self.font.pointSize]]; 
} 

당신의 font 당신이 가족의 모든 fonts 인쇄해야 작동하지 않는, 그래서 당신이 잘못 fontName 쓴 경우에 당신이 볼 수 있다면. 이 방법을 사용하면 bold 글꼴을 사용할 수 있는지 확인할 수도 있습니다. 인쇄되지 않으면이 옵션이 없습니다.

NSLog (@"Available Fonts: %@", [UIFont fontNamesForFamilyName:@"Your Font"]); 

font 물건 : https://stackoverflow.com/a/8529661/1141395

2

당신은이 사이트 iOSfonts에서 글꼴 이름을 지원하는 모든 아이폰 OS를 얻을 수 있습니다. 최고의 글꼴을 찾기 위해 정확한 문자열의 미리보기를 가지고 이미 응용 프로그램에 글꼴을 추가 한 경우는 위의 답변

UILabel * myLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 400, 150, 50)]; 
    [loading setFont:[UIFont fontWithName:@"Avenir-Black" size:12.0]]; 
    [loading setTextColor:[UIColor redColor]]; 
    [loading setBackgroundColor:[UIColor clearColor]]; 
    [loading setText:@"My Label Title"]; 
    [self.view myLabel]; 
0

스위프트 갱신

에 언급 좋아 사용 (로는 here 설명)는, 예를 들어,와 UILabel와 Roboto로 글꼴 스위프트의 extension 기능을 사용할 수 있습니다 :

extension UILabel { 

    func setFont(style: String, size: Int){ 
     self.font = UIFont(name: style, size: CGFloat(size)) 
    } 

    struct FontStyle { 
     public static let italic: String = "Roboto-LightItalic" 
     public static let normal: String = "Roboto-Light" 
     public static let thin: String = "Roboto-Thin" 
    } 
} 

Roboto-LightItalicRoboto-Light은 TTF의 글꼴 파일 이름입니다. 그러면 간단히 전화 할 수 있습니다

someUILabel.setFont(style: UILabel.FontStyle.normal, size: 20) 
관련 문제