我有一个UIWebView,并且我有一个自定义字体,我想加载到可编辑的HTML文档中,然后通过UIWebView显示。在我的SupportingFiles(资源)文件夹中,我有“Nosifer-Regular.ttf”…为了在我的HTML中使用自定义字体,我需要该字体文件的路径(URL)…我尝试这样做,但似乎没有起作用…有什么想法吗?
bundle = [NSBundle mainBundle];
pathFont = [bundle bundlePath];
fontURL = [NSBundle pathForResource:@"Nosifer-Regular" ofType:@"ttf" inDirectory:pathFont];
path_font = [fontURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
fileAppend = [[NSString alloc] initWithFormat:@"file://"];
path_font = [[NSString alloc] initWithFormat:@"%@%@", fileAppend, path_font];
HTML(CSS):
@font-face {
font-family: 'Nosifer';
font-style: normal;
font-weight: 400;
src: local('Nosifer'), local('Nosifer-Regular'), url('%@') format('truetype');
}
其中的 %@ 被替换为“path_font”(在第一个代码块中定义)
baseURL
参数指向你的应用程序资源文件夹。 - nielsbot