4

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

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”(在上面的第一个代码块中定义)

4

4 回答 4

4

首先,在您的info.plist. 这将是一个数组

item0 myFont.otf
item1 anotherFont.ttf

或者其他的东西。

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

祝你好运。

于 2012-01-26T05:50:34.993 回答
1

我在这里得到了一个有效的 html 代码示例。

<html>
<head>
    <style type='text/css'>
        p.p1 {margin: 0.0px 0.0px 0.0px 30.0px; font: 34.0px;}
        @font-face {
            font-family: 'Nosifer'; font-style: normal; font-weight: 400; src: local('Nosifer'), local('Nosifer-Regular'), url('Nosifer-Regular.ttf') format('truetype');
        }

        .nosifer {
            font-family: Nosifer;
        }

        </style>
</head>
<body allowtransparency='true'>
    <p class="nosifer">Nosifer ABCabc</p>
</body>

如您所见,我使用了一个 css 类来更轻松地定义字体。我不确定 font-face 中的所有 CSS 代码是什么意思,但它似乎有效。

我确认您需要在* -info.plist 文件中将“Nosifer-Regular.ttf”添加到应用程序提供的字体数组中(键:应用程序提供的字体)。顺便说一句,我用谷歌搜索了 Nosifer-Regular.ttf 并在这里的一个嘈杂的网站上找到了它。它确实有效:http ://www.ffonts.net/Nosifer.font.download

然后,我还能够加载我将在我的案例中使用的 OTF。在 CSS 中,我将格式从 truetype 更改为 opentype。

于 2012-08-29T06:36:05.537 回答
1

在您的项目中添加您的自定义字体。然后

获取字体名称

 UIFont *font = [UIFont fontWithName:@"Your-Custom-font" size:20];

然后创建html字符串

NSString *htmlHeader=[NSString stringWithFormat:@"<html><head><head><link rel=\"stylesheet\" type=\"text/css\" href=\"file://localhost%@\"></head><body><font face=\"%@\"; size=\"4\">HI</font></body></html>",cssPath,font.familyName];

祝你好运。

于 2014-10-20T15:26:00.333 回答
0

以下是将字体添加到 Info.plist 的方法:

    <key>UIAppFonts</key>
    <array>
        <string>Nosifer-Regular.ttf</string>
    </array>

然后按照其他答案使用@font-face 。

于 2012-01-26T05:52:29.807 回答