繁体   English   中英

具有自定义字体类型和颜色的 WKWebView

[英]WKWebView with custom font type and color

我想在 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"];

首先,您可以创建一个 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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM