【问题标题】:WKWebView with custom font type and color具有自定义字体类型和颜色的 WKWebView
【发布时间】:2017-03-08 10:33:11
【问题描述】:

我想在 WKWebView 加载带有自定义字体系列和文本颜色的 html 字符串,我尝试将此 CSS 嵌入 HTML 但它不起作用:( .

 [NSString stringWithFormat:@"<html> \n"
                                   "<head> \n"
                                   "<style type=\"text/css\"> \n"
                                   "body {font-family: \"%@\"; font-size: %d;}\n"
                                   "</style> \n"
                                   "</head> \n"
                                   "<body>%@</body> \n"
                                   "</html>", @"DINNextLTW23Regular", self.fontSize,@"Text"];

【问题讨论】:

标签: ios objective-c webkit wkwebview


【解决方案1】:

首先你可以创建一个css文件,然后在这个文件中添加你的字体和样式

@font-face {
    font-family: 'FONT_FAMILY';
    src: local('FONT_FAMILY'),url('FONT_FILE_NAME.otf') format('opentype');
}

h1 {
    font-family: 'FONT_FAMILY';
    font-size: 20px;
    font-weight: normal;
}

并用css文件加载html字符串

NSString *css = [NSString stringWithFormat:@"<head>"
                        "<link rel=\"stylesheet\" type=\"text/css\" href=\"YOUR_CSS_FILE.css\">"
                        "</head>"];
NSString *content = [NSString stringWithFormat:@"<body>"
                        "<h1>%@</h1>"
                        @"</body>", @"YOUR_TEXT"];

[_wkWebView loadHTMLString:[NSString stringWithFormat:@"%@%@", css, content]
                   baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"YOUR_CSS_FILE" ofType:@"css"]]];

你可以用你的改变样式名h1

【讨论】:

  • 经过数小时的搜索,兄弟,你让我很开心!!
【解决方案2】:

font-family 应该是系统字体系列。否则,您必须将自定义字体文件导入您的项目。

【讨论】:

  • xxx.plist文件中,需要添加:` UIAppFontsYour_Font_File_Name.TTF`
猜你喜欢
  • 1970-01-01
  • 2019-02-07
  • 1970-01-01
  • 1970-01-01
  • 2020-06-01
  • 2021-04-18
  • 2014-02-15
  • 1970-01-01
  • 2014-12-15
相关资源
最近更新 更多