development

스토리 보드의 사용자 지정 글꼴?

big-blog 2020. 11. 22. 20:01
반응형

스토리 보드의 사용자 지정 글꼴?


iOS 프로젝트에 글꼴이 추가되었습니다. 프로젝트에 있으며 프로젝트가 빌드 될 때 복사됩니다. 유효한 글꼴 인 것으로 보이며 앱을 통해 기기의 모든 글꼴을 나열하면 표시됩니다. 글꼴을 포함하도록 plist를 올바르게 설정했습니다. Xcode 4.2의 스토리 보드 섹션에있는 텍스트 컨트롤, 버튼 등에서 사용하기 위해 찾을 수없는 것 같습니다. 또한 글꼴 이름을 입력 할 수없는 것처럼 보이기 때문에 글꼴 대화 상자를 사용해야합니다. 시스템에도이 글꼴을 설치하려고했지만이 목록에 표시되지 않는 것 같습니다. 코드로만이 작업을 수행해야합니까? 아니면 스토리 보드 인터페이스를 통해 수행 할 수 있습니까?

코드에서이 작업을 수행 할 수 있지만 스토리 보드를 통해 수행하는 것이 훨씬 더 편리 할 것입니다.


업데이트 : Xcode 6 Interface Builder는 이제 사용자 정의 글꼴을 선택하고 디자인 타임에 올바르게 렌더링 할 수 있습니다.

이 질문이 꽤 오래되었다는 것을 알고 있지만 iOS 5에서 Storyboard (또는 Interface Builder)에서 사용자 지정 글꼴을 쉽게 지정할 수있는 쉬운 방법을 찾기 위해 고군분투했으며 매우 편리한 솔루션을 찾았습니다.

먼저이 튜토리얼 에 따라 프로젝트에 글꼴을 추가했는지 확인합니다 . 또한 UINavigationBar, UITabBar 및 UISegmentedControl 사용자 지정 글꼴은 UIAppearance 프록시setTitleTextAttributes:메서드를 사용하여 지정할 수 있습니다 .

UIButton, UITextField, UILabel 및 사용자 정의 글꼴이 필요한 기타 구성 요소에 대한 프로젝트에 아래 카테고리를 추가하십시오. 카테고리는 단순히 fontName글꼴 크기를 유지하면서 요소의 현재 글꼴을 변경 하는 새 속성 구현합니다 .

스토리 보드에서 글꼴을 지정하려면 원하는 요소 (라벨, 버튼, 텍스트보기 등)를 선택하고 키 경로가 문자열 유형의 fontName 으로 설정된 사용자 정의 런타임 속성사용자 정의 글꼴 이름이있는 값을 추가 하기 만하면 됩니다.

커스텀 폰트 스토리 보드

그게 전부입니다. 카테고리를 가져올 필요조차 없습니다. 이렇게하면 사용자 지정 글꼴이 필요한 모든 UI 구성 요소에 대한 콘센트가 필요하지 않으며 수동으로 코딩 할 필요가 없습니다.

글꼴은 스토리 보드에 표시되지 않지만 장치 또는 시뮬레이터에서 실행할 때 표시된다는 점을 고려하십시오.


카테고리 파일

UIButton + TCCustomFont.h :

#import <UIKit/UIKit.h>

@interface UIButton (TCCustomFont)
@property (nonatomic, copy) NSString* fontName;
@end

UIButton + TCCustomFont.m :

#import "UIButton+TCCustomFont.h"

@implementation UIButton (TCCustomFont)

- (NSString *)fontName {
    return self.titleLabel.font.fontName;
}

- (void)setFontName:(NSString *)fontName {
    self.titleLabel.font = [UIFont fontWithName:fontName size:self.titleLabel.font.pointSize];
}

@end

UILabel + TCCustomFont.h :

#import <UIKit/UIKit.h>

@interface UILabel (TCCustomFont)
@property (nonatomic, copy) NSString* fontName;
@end

UILabel + TCCustomFont.m :

#import "UILabel+TCCustomFont.h"

@implementation UILabel (TCCustomFont)

- (NSString *)fontName {
    return self.font.fontName;
}

- (void)setFontName:(NSString *)fontName {
    self.font = [UIFont fontWithName:fontName size:self.font.pointSize];
}

@end

UITextField + TCCustomFont.h :

#import <UIKit/UIKit.h>

@interface UITextField (TCCustomFont)
@property (nonatomic, copy) NSString* fontName;
@end

UITextField + TCCustomFont.m :

#import "UITextField+TCCustomFont.h"

@implementation UITextField (TCCustomFont)

- (NSString *)fontName {
    return self.font.fontName;
}

- (void)setFontName:(NSString *)fontName {
    self.font = [UIFont fontWithName:fontName size:self.font.pointSize];
}

@end

GIST 에서 단일 파일도 다운로드 할 수 있습니다 .

문제 해결

fontName속성이 지정되지 않아 런타임 오류에 대해 실행하는 경우 프로젝트 설정의 기타 링커 플래그 -all_load아래 에 플래그를 추가 하여 링커가 범주를 포함하도록 강제합니다.


Xcode6.0에서 Xcode6.0 릴리스 노트로 :

Interface Builder는 디자인 타임에 임베드 된 맞춤형 iOS 글꼴을 렌더링하여 완성 된 앱의 모양을 정확한 치수로보다 정확하게 미리 볼 수 있습니다.

스토리 보드에서 레이블 글꼴을 설정할 수 있습니다.

  1. 사용자 정의 글꼴 파일 ( .ttf, .ttc) 가져 오기
  2. Xcode 프로젝트로 글꼴 파일 가져 오기

    여기에 이미지 설명 입력

  3. 에서 앱의 Info.plist , 열쇠라는 이름의 추가 응용 프로그램에 의해 제공되는 글꼴 , .IT의 배열 형식을 배열에 대한 모든 글꼴 파일 이름을 추가, 참고 : 파일 확장자를 포함.

여기에 이미지 설명 입력

  1. 스토리 보드에서 UILabel을 인터페이스로 드래그하고, 레이블을 선택하고, Attribute Inspector로 이동하고 , 글꼴 선택 영역 의 오른쪽 아이콘 버튼을 클릭합니다 . 팝업 패널에서 Font to Custom 을 선택하고 포함 된 Family선택합니다. 글꼴 이름.

여기에 이미지 설명 입력

참조


이것은 XCode의 버그이며 3.x 이후에 있었지만 아직 수정되지 않았습니다. 나도 같은 문제에 직면했고 심지어 운없이 내 시스템에 추가하려고 시도했습니다. 인터페이스 빌더에 글꼴이 표시되지 않는 다른 SO 게시물이 있습니다.


Monjer's answer is the correct one. If you, like me, don't see the added font after compiling, make sure to add the font's to the build phases (targets -> build phases -> copy bundle resources -> add your font files)


As of XCode 9.4, XCode still does not respect fonts directly in the storyboard. You can see them at design time, but not at runtime. If you are using fontWithName API, you get to know it returns nil, but when used in storyboard/xib, there is no way to know why it doesn't appear.

  • The only way to make sure it works readily from Storyboard/XIB at runtime is to add .ttc/.ttf into Copy Bundle Resources Build Phase.

  • As of 9.4, adding font file names to info.plist seems no longer a requirement.


redent84's solution was awesome!

개선 사항을 추가하고 NSObject에 범주를 추가하므로 한 번만 수행하면됩니다.

NSObject + CustomFont.h

#import <Foundation/Foundation.h>

@interface NSObject (CustomFont)

@property (strong, nonatomic) NSString *fontName;
@property (strong, nonatomic) UIFont *font;

@end

NSObject + CustomFont.m

#import "NSObject+CustomFont.h"

@implementation NSObject (CustomFont)

@dynamic font;

- (NSString *)fontName
{
    return self.font.fontName;
}

- (void)setFontName:(NSString *)fontName
{
    self.font = [UIFont fontWithName:fontName size:self.font.pointSize];
}

@end

이것들 중 어느 것도 나를 위해 일하지 않았지만 이것은 효과가있었습니다.

#import <UIKit/UIKit.h>

@interface UILabelEx : UILabel


@end

#import "UILabelEx.h"
#import "Constants.h"

@implementation UILabelEx

- (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection {
    [super traitCollectionDidChange: previousTraitCollection];

    self.font = [UIFont fontWithName:APP_FONT size:self.font.pointSize];   
}
@end

참고 URL : https://stackoverflow.com/questions/9090745/custom-font-in-a-storyboard

반응형