UIWebViews 的 iOS 自定义字体路径

2023-11-09移动开发问题
2

本文介绍了UIWebViews 的 iOS 自定义字体路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个 UIWebView,我有一个自定义字体我想加载到一个可编辑的 html 文档中,我将通过 UIWebView 显示它.在我的 SupportingFiles(资源)文件夹中,我有Nosifer-Regular.ttf"...要在我的 HTML 中使用自定义字体,我需要字体拼贴的路径(URL)...我试过这样做,但它没有似乎不起作用...有什么想法吗?

I have a UIWebView and I have a custom font I want to load into an editable html document that I will be displaying through the UIWebView. In my SupportingFiles (resources) folder I have "Nosifer-Regular.ttf"... To use the custom font in my HTML I need the path (URL) to the font tile... I tried doing this, but it didn't seem to work... any ideas?

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):

The HTML (CSS):

@font-face {
  font-family: 'Nosifer';
  font-style: normal;
  font-weight: 400;
  src: local('Nosifer'), local('Nosifer-Regular'), url('%@') format('truetype');
}

%@ 替换为path_font"(在上面的第一个代码块中定义)

Where %@ is replaced with "path_font" (defined above in the first code block)

推荐答案

首先,在你的info.plist中添加应用程序提供的字体"字段.它将是一个类似

First of all, add filed "Fonts provided by application" in your info.plist. It will be an array like

item0 myFont.otf
item1 anotherFont.ttf

什么的.

在 HTML 中使用 font-face 添加字体.并将您的字体文件与 html 文件放在同一目录中.如果 HTML 是动态生成的并且没有从 bundle 中显示,那么你应该复制字体.

In HTML use font-face to add a font. And place your font files in the same directory with html file. If HTML is generated dynamically and not shown from bundle then you should copy font .

祝你好运.

这篇关于UIWebViews 的 iOS 自定义字体路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

[ios.cocos2d+box2d]如何禁用自动旋转?
[ios.cocos2d+box2d]how to disable auto-rotation?([ios.cocos2d+box2d]如何禁用自动旋转?)...
2024-08-12 移动开发问题
7

从 Documents 目录存储和读取文件 iOS 5
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)...
2024-08-12 移动开发问题
9

如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?
How can I implement a virtual joystick for a cocos2d game outside the cocos2d environment?(如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?)...
2024-08-12 移动开发问题
13