iOS中字型的使用

2021-07-14 17:16:03 字數 3703 閱讀 8978

有三種方式:

一、使用系統隨附的字型

在使用之前,先把支援的所有字型列印出來,確保存在再使用:

// 列印所有的系統已存在的字型

static

void dumpallfonts()

}nslog(@"totalcount=%ld",totalcount);

}

使用就很簡單了:

[uifont fontwithname:@"字型的名字" size:14]
二、從外界拖進來的靜態字型(也就是你的ui給你的字型,.ttf或者.ttc等格式的檔案)

在使用前雙擊資源檔案,mac會預設用『字型冊』開啟,這時你會看到一些相關資訊:

/**

*/#import

#import

typedef

void(^downloadingblock)(cgfloat progressvalue);

typedef

void(^downloadsuccessblock)(void);

typedef

void(^**********rocblock)(nsstring *errormsg);

@inte***ce

asydownloadsystemfont : nsobject

/** *

* @param fontname 字型名字 (不是資料夾的名字)

* */

+(bool)i***istwithfontname:(nsstring *)fontname;

/** *

* @param fontname 字型名字 (不是資料夾的名字)

*/+(void)asydownloadfontwithfontname:(nsstring *)fontname

downloading:(downloadingblock)loadingcallback

downloadsuccess:(downloadsuccessblock)sucesscallback

**********ror:(**********rocblock)errorcalback;

@end

#import "asydownloadsystemfont.h"

#import

@implementation

asydownloadsystemfont

#pragma mark - public methods

+(bool)i***istwithfontname:(nsstring *)fontname

nslog(@"不存在");

return

no;}

+(void)asydownloadfontwithfontname:(nsstring *)fontname

downloading:(downloadingblock)loadingcallback

downloadsuccess:(downloadsuccessblock)sucesscallback

**********ror:(**********rocblock)errorcalback else

if (state == kctfontdescriptormatchingdidfinish)

});}

}else

if (state == kctfontdescriptormatchingwillbegindownloading) else

if (state == kctfontdescriptormatchingdidfinishdownloading)

});}else

if (state == kctfontdescriptormatchingdownloading)

});}else

if (state == kctfontdescriptormatchingdidfailwitherror) else

dispatch_async(dispatch_get_main_queue(), ^

});nslog(@"errormsg=%@",errormsg);

//設定標誌

errorduringdownload = yes;

}return

yes;

});}@end

下面就是具體的使用:

#import "dynamicviewcontroller.h"

#import "asydownloadsystemfont.h"

#import "uiview+framehelp.h"

static

nsarray *kfontnames()

return fontnames;

}static

nsarray *kfontsamples()

return fontsamples;

}@inte***ce

dynamicviewcontroller ()

@property (nonatomic, retain) uitableview *ktableview;

@property (nonatomic, retain) uiprogressview *kprogress;

@property (nonatomic, retain) uitextview *ktextview;

@property (nonatomic, retain) uiactivityindicatorview *kactivityview;

@end

@implementation

dynamicviewcontroller

#pragma mark - life cycle

- (void)viewdidload

#pragma mark - uitableviewdatasource

-(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section

-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath

cell.textlabel

.text = [fontname objectatindex:indexpath.row];

return cell;

}#pragma mark - uitableviewdelegate

-(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath else downloadsuccess:^ **********ror:^(nsstring *errormsg) ];

}}-(void)refrshtextviewwithtext:(nsstring *)text

#pragma mark - private methods

-(void)uiconfig

@end

iOS 使用新增的花樣字型

專案需求中,有時候有些金額利率等這些不用系統預設字型展現,而需要著重突出展示 將字型庫新增到專案中 在info.plist中新增所需新字型,同時也在target build phases copy bundle resources中將info.plist中新增的字型新增上 確保能找到資源 找到真實可...

IOS中怎麼使用微軟雅黑字型

網上搜尋字型檔案 字尾名為.ttf,或.odf 2.把字型庫匯入到工程的resouce中 3.在程式viewdidload中載入一下一段 nsarray familynames uifont familynames for nsstring familyname in familynames 4.假...

iOS使用自定義字型

要新增方正蘭亭字型,首先需要乙個ttf的字型庫。然後把這個檔案新增到工程裡面 在plist裡面新增 新增成功後,就可以使用啦。但是我只拿到這個庫,並不清楚fontname。用下面這段 列印出來系統可用字型。nsarray familynames nsarrayalloc initwitharray ...