zoukankan      html  css  js  c++  java
  • iOS添加自定义字体方法

    1:获取字体文件

    从各种渠道下载字体文件ttf, 网站或者从别的ipa里扣出来.(以fzltxh.ttf为例)

    2:将fzltxh.ttf文件拷贝到工程中

    3:在Info.plist中添加项:

    Fonts provided by application(UIAppFonts)  可以添加一个或多个item,

    如 item0 --  fzltxh.ttf

    4:找出真正的字体名称:

    因为使用字体时, 要使用字体的真实名称, 而不是文件名, 可以用以下代码来遍历当前设备可用的字体名称,

    再从中找出刚才添加的字体真实名称.

        
        NSArray *familyNames = [UIFont familyNames];
        for( NSString *familyName in familyNames )
        {
            printf( "Family: %s 
    ", [familyName UTF8String]);
            
            NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
            for( NSString *fontName in fontNames )
            {
                printf( "	Font: %s 
    ", [fontName UTF8String] );
            }
        }
    

      

    FZLTXHK--GBK1-0  这个就是此字体的真实使用名称.

     

    5:使用字体

    [UIFont fontWithName:@"FZLTXHK--GBK1-0" size:fontSize];
    

      

    6:统一替换

    如果想把旧工程的字体整体替换掉, 又不想改动已有代码, 可以重写 

    systemFontOfSize 方法.

    //
    //  UIFont+custom.h
    //  TuJing
    //
    //  Created by willbin on 15/1/13.
    //  Copyright (c) 2015年 willbin. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface UIFont (TJCustom)
    
    + (UIFont *)systemFontOfSize:(CGFloat)fontSize;
    
    @end
    
    //
    //  UIFont+custom.m
    //  TuJing
    //
    //  Created by willbin on 15/1/13.
    //  Copyright (c) 2015年 willbin. All rights reserved.
    //
    
    #import "UIFont+custom.h"
    
    @implementation UIFont (TJCustom)
    
    + (UIFont *)systemFontOfSize:(CGFloat)fontSize
    {
        return [UIFont fontWithName:@"FZLTXHK--GBK1-0" size:fontSize];
    }
    
    @end
    

      

    这样的话, 原先写的 

    systemFontOfSize 方法都会用新方法代替, 从而实现整体替换的效果.

     

     tips:

    字体名字找不到?
    1. 双击导入到Mac系统时, 会有信息显示.

    2. 在工程中打印当前字体列表, 对比添加前后的差异就知道了.

  • 相关阅读:
    LeetCode 1110. Delete Nodes And Return Forest
    LeetCode 473. Matchsticks to Square
    LeetCode 886. Possible Bipartition
    LeetCode 737. Sentence Similarity II
    LeetCode 734. Sentence Similarity
    LeetCode 491. Increasing Subsequences
    LeetCode 1020. Number of Enclaves
    LeetCode 531. Lonely Pixel I
    LeetCode 1091. Shortest Path in Binary Matrix
    LeetCode 590. N-ary Tree Postorder Traversal
  • 原文地址:https://www.cnblogs.com/willbin/p/4220810.html
Copyright © 2011-2022 走看看