Programing

iOS 7의 사용자 정의 글꼴

lottogame 2020. 8. 15. 09:43
반응형

iOS 7의 사용자 정의 글꼴


게임을 개발 중이며 앱에서 사용자 지정 글꼴을 사용하고 싶습니다. 중요한 경우 SpriteKit을 사용하고 있습니다. https://github.com/deni2s/IBCustomFonts를 사용해 보았지만 이 글꼴 http://www.fontspace.com/freaky-fonts/emulogic 으로 작동하도록 할 수 없습니다 .

다양한 시도를 해봤고 앱의 info.plist에 폰트를 등록했습니다 ... 무슨 일인지 아는 사람 있나요?


우선 저는 SpriteKit이 아무런 차이를 만들지 않는다고 가정합니다.

  1. 프로젝트에 복사 한 .otf 또는 .ttf 글꼴이 필요합니다. 예를 들어 지원 파일에서.
  2. .plist 파일 을 편집해야 합니다. 추가 "응용 프로그램에서 제공하는 글꼴" 당신의 plist로와의 주요 항목 0은 당신이 확장자를 가진 당신의 지원 파일을 복사 한 글꼴의 정확한 파일 이름을 복사합니다. 예 : "JosefinSansStd-Light_0.otf"
  3. 앱으로 가져온 글꼴이 앱 자체에 압축되어 있는지 확인하십시오. 당신의 선택에 의해 그렇게 할 대상 , 다음 페이즈를 구축 한 다음 복사 번들 리소스 . 글꼴이 보이지 않으면 지원 파일 에서 드래그하십시오 .

마지막으로 앱이 시작될 때 글꼴에 사용할 수있는 이름을보기 위해 모든 글꼴을 나열하려고합니다. 이 작은 코드로 그렇게 할 것입니다.

NSArray *fontFamilies = [UIFont familyNames];
for (int i = 0; i < [fontFamilies count]; i++)
{
    NSString *fontFamily = [fontFamilies objectAtIndex:i];
    NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
    NSLog (@"%@: %@", fontFamily, fontNames);
}

인쇄 된 결과에서 글꼴을 검색합니다. 예를 들어 "Josefin"을 검색하면 실제 글꼴 이름이 "JosefinSansStd-Light"임을 알 수 있습니다. 그 후에는 다음과 같은 방법으로 만 해당 글꼴을 사용해야합니다.

UIFont *customFont = [UIFont fontWithName:@"JosefinSansStd-Light" size:20];

iOS8에서는 글꼴 을 프로젝트에 직접 추가 하면 인터페이스 빌더에 표시됩니다. 이를 고려하여 코드를 수정하지만 프로그래밍 방식으로 iOS7 용 글꼴을 설정하고 xCode6 인터페이스 빌더에서 선택합니다. 추신. xCode6의 인터페이스 빌더는 아래 코드에 복사하여 붙여 넣을 수있는 올바른 글꼴 이름을 제공합니다.

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

 if(SYSTEM_VERSION_LESS_THAN(@"8.0"))
    {
        UIFont *customFont = [UIFont fontWithName:@"OpenSans-Light" size:32];
        self.registerLabel.font = customFont;  
    }

이것이 도움이되기를 바랍니다.


사용자 정의 글꼴을 사용하는 데 문제가 없습니다. 다음과 같이하세요:

  1. "응용 프로그램에서 제공하는 글꼴"항목을 추가하여 info.plist에 글꼴을 등록합니다. 글꼴 이름이 아닌 글꼴 파일 이름을 입력하십시오. font.ttf처럼 (이미 그랬다고 생각합니다)
  2. 프로젝트 탐색기에서 프로젝트로 이동하여 프로젝트 이름을 클릭하고 "빌드 단계"로 이동 한 다음 "번들 리소스 복사"에서 아래로 이동하여 맨 아래로 이동하면 많은 파일이 있으며 "+"기호를 검색합니다. 프로젝트 파일이 포함 된 팝업 창이 열립니다. 글꼴 파일을 검색하여 번들에 추가하십시오.
  3. 마지막으로 글꼴 이름을 확인하십시오. 코드에서 사용할 때는 파일 이름이 아닌 글꼴 이름을 사용해야합니다. 글꼴 이름을 확인하려면 Finder에서 글꼴 파일을 두 번 클릭하면 글꼴이 표시된 창이 열립니다. 창 제목을 확인하십시오. 이것이 사용해야하는 글꼴의 이름입니다.

그리고 조언은 ..Prefix.pch 파일에 해당 이름을 매크로로 추가하십시오. 글꼴을 여러 번 사용해야하는 경우 프로젝트에서 사용하기가 더 쉽습니다.

문제는 글꼴을 제대로 등록했지만 앱 번들에 복사되지 않았다는 것입니다. 따라서 앱이 실행될 때 글꼴을 찾을 수 없습니다. 이것이 2 단계가 중요한 이유입니다. 나는 처음으로 그것을 알아 내려고 많은 시간을 보냈다. :)


이를 신속하게 달성하려면

앱이 시작될 때 글꼴의 사용 가능한 이름을보기 위해 모든 글꼴을 나열하려는 경우. 이 작은 코드로 그렇게 할 수 있습니다.

    UIFont.familyNames.forEach {
        print("fontFamily: \($0), fonts: \(UIFont.fontNames(forFamilyName: $0))")
    }

여러 번 시도한 결과 이러한 답변을 얻을 수 없었습니다. 허용 된 답변의 지침을 따르고 글꼴을 사용하는 스토리 보드에 숨겨진 레이블을 만드는 유일한 방법은 사용자 지정 글꼴을 얻었습니다. 프로그래밍 방식으로 글꼴을 사용할 수 있습니다. 내가 아는 이상하고 해키하지만 그것은 나를 위해 일했습니다.


If you're in Xcode 6 it's as easy as it gets. Just add a font folder into your project, add your font(s) into the folder, and you're good to go.

enter image description here


If none of the methods above worked for you, AND you happen to use your custom font "programatically" only (i.e. you didn't use it in any xib) then you will need to use it at least once somewhere in one xib, anywhere.

(P.S. Stuart Clark - already answered this, so no credit for me please. However, I decided to rephrase and post it as a separate answer, because I missed his answer, it wasn't clear to me when I read it. I found out this solution by chance when I was testing the custom font in a xib, and I saw that it worked elsewhere programatically, but when I removed the font from the xib, it stopped working programatically.)


Updated for Swift 3.

let fontFamilies:[String] = UIFont.familyNames

for fontFamily: String in fontFamilies {
    let fontNames: [String] = UIFont.fontNames(forFamilyName: fontFamily)
    print("\(fontFamily)  \(fontNames)");
}

Maybe a quick fix - this did it for me when the font has already worked, but a new e.g. Label didn't show the desired font:

clean, deep clean and empty derived data folder.


please try below code.
@end
@implementation UILabel (CustomFont)
-(void)setFontName:(NSString *)value{
CGFloat newsize = (self.font.pointSize * [UIScreen mainScreen].bounds.size.width)/320;
[self setFont:[UIFont fontWithName:self.font.fontName size:newsize]];
}
@end
@implementation UITextField (CustomFont)
-(void)setFontName:(NSString *)value{
CGFloat newsize = (self.font.pointSize * [UIScreen mainScreen].bounds.size.width)/320;
[self setFont:[UIFont fontWithName:self.font.fontName size:newsize]];
}
@end
@implementation UIButton (CustomFont)
-(void)setFontName:(NSString *)value{
CGFloat newsize = (self.titleLabel.font.pointSize * [UIScreen mainScreen].bounds.size.width)/320;
[self.titleLabel setFont:[UIFont fontWithName:self.titleLabel.font.fontName size:newsize]];
}
@end
@end

@interface UILabel (CustomFont)
-(void)setFontName:(NSString *)value;
@end
@interface UITextField (CustomFont)
-(void)setFontName:(NSString *)value;
@end
@interface UIButton (CustomFont)
-(void)setFontName:(NSString *)value;
@end

참고URL : https://stackoverflow.com/questions/21737788/custom-fonts-in-ios-7

반응형