问题描述
我想在 UIWebView 中显示自定义字体.我已经将字体放在应用程序提供的字体"下的 plist 中.正在使用的代码:
I would like to display a custom font inside a UIWebView. I have already put the font in the plist under "Fonts provided by application". The code in use:
UIWebView *webView = [[UIWebView alloc] initWithFrame:myRect];
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[webView loadHTMLString:html baseURL:baseURL];
[self addSubview:webView];
其中 html 是一个 NSString,其内容如下:
where html is an NSString that has the following contents:
<html><head>
<style type="text/css">
@font-face {
font-family: gotham_symbol;
src: local('GOTHAMboldSymbol_0.tff'), format('truetype')
}
body {
font-family: gotham_symbol;
font-size: 50pt;
}
</style>
</head><body leftmargin="0" topmargin="0">
This is <i>italic</i> and this is <b>bold</b> and this is some unicode: э
</body></html>
我使用的是 iOS 4.2,所以应该支持 TTF.我很感激一些真正有效的 html/代码.
I'm using iOS 4.2 so TTF should be supported. I'd appreciate a bit of html/code that actually works.
推荐答案
经过一些尝试和错误,我找到了一种使用本地 CSS 加载自定义字体的可靠方法.
After some Try and Error I have found a reliable way to load custom Fonts with a local CSS.
1.将您的字体添加到应用程序...确保该文件是有针对性的正确应用程序
1. Add your Font to the App...make sure that the file is targeted properly to the Application
2.然后将您的字体添加到 yourApp-Info.plist
2. Then add your Font to yourApp-Info.plist
3.运行 NSLog(@"Available fonts: %@", [UIFont familyNames]);
要检查字体/字体系列的名称为系统...
3. Run NSLog(@"Available fonts: %@", [UIFont familyNames]);
To check which name the font/fontfamily has for the System...
4.复制该名称并在您的 CSS 中使用它们...不需要@font-face
4. Copy that name and use them in your CSS...@font-face is not needed
body {
font-family:"Liberation Serif";
}
这篇关于在 UIWebView 中使用自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!