2017-03-08 41 views
1

我想加载HTML字符串自定义字体家人和文本颜色在WKWebView,我想在HTML中嵌入这个CSS,但它不工作:(。WKWebView与自定义字体类型和颜色

[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"]; 
+0

检查答案在这里:[http://stackoverflow.com/questions/449773/iphone-development-setting- uiwebview-font](http://stackoverflow.com/questions/449773/iphone-development-setting-uiwebview-font) –

回答

2

首先你可以创建一个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; 
} 

并加载HTML字符串的CSS文件

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与你

-1

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

+0

已经导入到应用程序,我将它包含在.plist文件中 –

+0

在'xxx.plist'文件中,您需要添加: ' UIAppFonts \t Your_Font_File_Name.TTF ' – Andney