2017-03-08 77 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