ios 在 UIWebView 中使用自定义字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10490696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using custom font in a UIWebView
提问by Joris Weimar
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 中显示自定义字体。我已经将字体放在“应用程序提供的字体”下的 plist 中。使用中的代码:
UIWebView *webView = [[UIWebView alloc] initWithFrame:myRect];
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[webView loadHTMLString:html baseURL:baseURL];
[self addSubview:webView];
where html is an NSString that has the following contents:
其中 html 是具有以下内容的 NSString:
<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>
I'm using iOS 4.2 so TTF should be supported. I'd appreciate a bit of html/code that actually works.
我使用的是 iOS 4.2,所以应该支持 TTF。我很欣赏一些实际有效的 html/代码。
回答by Lindemann
After some Try and Error I have found a reliable way to load custom Fonts with a local CSS.
经过一些尝试和错误后,我找到了一种使用本地 CSS 加载自定义字体的可靠方法。
1. Add your Font to the App...make sure that the file is targeted properly to the Application
1. 将您的字体添加到应用程序...确保文件正确定位到应用程序
2. Then add your Font to yourApp-Info.plist
2.然后将您的字体添加到 yourApp-Info.plist
3. Run NSLog(@"Available fonts: %@", [UIFont familyNames]);
To check which name the font/fontfamily has for the System...
3.运行NSLog(@"Available fonts: %@", [UIFont familyNames]);
以检查字体/字体系列的名称为系统...
4. Copy that name and use them in your CSS...@font-face is not needed
4. 复制该名称并在您的 CSS 中使用它们...不需要@font-face
body {
font-family:"Liberation Serif";
}
回答by Joris Weimar
I ended up making it work although I'm not sure what I did wrong above. Here's the HTML I use (NSString *htmll). I added everything, some of it might not be relevant for the solution.
虽然我不确定我上面做错了什么,但我最终让它工作了。这是我使用的 HTML (NSString *htmll)。我添加了所有内容,其中一些可能与解决方案无关。
<html><head><style type="text/css">
@font-face {
font-family: 'gotham_symbol';
src: url('GOTHAMboldSymbols_0.ttf') format('truetype')
}
@font-face {
font-family: 'gotham_symbol_italic';
src: url('GothamBoldItalic.ttf') format('truetype')
}
#w {display:table;}
#c {display:table-cell; vertical-align:middle;}
i { font-family: 'Helvetica-BoldOblique'; }
</style></head><body topmargin="0" leftmargin="0">
<div style="display: table; width: 320px; height: 50px; #position: relative; overflow: hidden;">
<div style=" #position: absolute; #top: 50%;display: table-cell; vertical-align: middle;">
<div style=" #position: relative; #top: -50%">
<p style="font-size:20px;font-family:'gotham_symbol';color:#97371f;">THE TEXT GOES HERE</p></div></div></div></body></html>
and I load the UIWebView as follows:
我按如下方式加载 UIWebView:
UIWebView *webView = [[UIWebView alloc] initWithFrame:rect];
[webView makeTransparent];
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[webView loadHTMLString:html baseURL:baseURL];