在UIWebView中使用自定义字体

编程入门 行业动态 更新时间:2024-10-26 02:25:20
本文介绍了在UIWebView中使用自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想在UIWebView中显示自定义字体。我已经将字体放在应用程序提供的字体下的plist中。正在使用的代码: pre $ $ $ c $ UIWebView * webView = [[UIWebView alloc] initWithFrame:myRect]; NSURL * baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; [webView loadHTMLString:html baseURL:baseURL]; [self addSubview:webView];

其中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 =0topmargin =0> 这是< i>斜体< / i>并且这是< b>粗体< / b>这是一个unicode:&#1101; < / body>< / html>

我使用的是iOS 4.2,所以应该支持TTF。我会很感激一些HTML /代码,实际上工作。

解决方案

经过一些尝试和错误,我找到了一个可靠的方法使用本地CSS加载自定义字体。

1。添加您的字体到应用程序...确保该文件的目标正确的应用程序

2。然后将你的字体添加到yourApp-Info.plist中

$ b

3。运行 NSLog(@Available fonts:%@,[UIFont familyNames]); 检查系统字体/ fontfamily的名称...

$ / 。复制该名称并在CSS中使用它们... @ font-face不是必需的 pre $ body $ {字体 - 家族:解放线; }

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];

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: &#1101; </body></html>

I'm using iOS 4.2 so TTF should be supported. I'd appreciate a bit of html/code that actually works.

解决方案

After some Try and Error I have found a reliable way to load custom Fonts with a local CSS.

1. Add your Font to the App...make sure that the file is targeted properly to the Application

2. Then add your Font to yourApp-Info.plist

3. Run NSLog(@"Available fonts: %@", [UIFont familyNames]); To check which name the font/fontfamily has for the System...

4. Copy that name and use them in your CSS...@font-face is not needed

body { font-family:"Liberation Serif"; }

更多推荐

在UIWebView中使用自定义字体

本文发布于:2023-11-07 22:30:26,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   字体   UIWebView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!