簡體   English   中英

如何更改 WKWebView 或 UIWebView 默認字體

[英]How to change WKWebView or UIWebView default font

UIWebViewWKWebView默認使用什么字體? 我希望能夠改變這一點。 但我不想在 html 字符串中執行此操作,而是希望使用以下內容:

// obj-c
[webView setFont:[UIFont fontWithName:@"GothamRounded-Bold" size:14]

// swift
webView.setFont(UIFont(name: "GothamRounded-Bold", size: 14))

有這樣的財產或其他方式嗎?

你可以使用你的UIFont對象(所以你可以更容易地設置和修改它),但是將 HTML 包裝在一個span 自 HTML 4.01起不推薦使用font標簽

UIFont *font = [UIFont fontWithName:@"GothamRounded-Bold" size:14];

假設您已經創建了NSString *htmlString ,您可以使用字體的屬性:

htmlString = [NSString stringWithFormat:@"<span style=\"font-family: %@; font-size: %i\">%@</span>",
                                        font.fontName,
                                        (int) font.pointSize,
                                        htmlString];

或者,您可以只提供值而不是使用UIFont對象:

htmlString = [NSString stringWithFormat:@"<span style=\"font-family: %@; font-size: %i\">%@</span>",
                                        @"GothamRounded-Bold",
                                        14,
                                        htmlString];

在加載到 webView 之前,只需在您的字符串前添加一個<font face>標簽。

NSString *body = [plist objectForKey:@"foo"];
NSString *htmlString = 
    [NSString stringWithFormat:@"<font face='GothamRounded-Bold' size='3'>%@", body];
[webView loadHTMLString:htmlString baseURL:nil];

您可以嘗試通過注入一行 JavaScript 來設置字體,如下所示:

[webView stringByEvaluatingJavaScriptFromString: @"document.body.style.fontFamily = 'GothamRounded-Bold'"];

有 swift 3 解決方案:

func webViewDidFinishLoad(_ webView: UIWebView) {
    webView.stringByEvaluatingJavaScript(from: "document.getElementsByTagName('body')[0].style.fontFamily =\"-apple-system\"")
}

我剛剛為這個例子添加了默認的 iOS 字體

這是我的易於使用且易於擴展的解決方案,具有更多字體設置:

myHTMLLabel = [[UIWebView alloc] initWithFrame:CGRectMake(myX, myY, myWidth, myHeight)];

myHTMLLabel.userInteractionEnabled=NO;
myHTMLLabel.scalesPageToFit=NO;

[myHTMLLabel setOpaque:NO];
myHTMLLabel.backgroundColor = myBackColor;

NSString *myHTMLText = [NSString stringWithFormat:@"<html>"
                        "<head><style type='text/css'>"
                        ".main_text {"
                        "   display: block;"
                        "   font-family:[fontName];"
                        "   text-decoration:none;"
                        "   font-size:[fontSize]px;"
                        "   color:[fontColor];"
                        "   line-height: [fontSize]px;"
                        "   font-weight:normal;"
                        "   text-align:[textAlign];"
                        "}"
                        "</style></head>"
                        "<body> <SPAN class='main_text'>[text]</SPAN></body></html>"];

myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[text]" withString: myText];
myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[fontName]" withString: myFontName];
myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[fontSize]" withString: myFontSize];
myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[fontColor]" withString: myFontColorHex];
myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[textAlign]" withString: myFontAlign];


NSLog(@"*** renderHTMLText --> myHTMLText: %@",myHTMLText);

[myHTMLLabel loadHTMLString:myHTMLText baseURL:nil];

如果有人在 Swift 3 中尋找解決方案,我使用了這個(基於 jterry 的回答):

let font = UIFont.init(name: "Name-of-your-font", size: theSize)
self.newsWebView.loadHTMLString("<span style=\"font-family: \(font!.fontName); font-size: \(font!.pointSize); color: #21367B\">\(yourString)</span>", baseURL: nil)

請注意,在這里我不確保字體不為零。 在加載 HTML 字符串之前可以添加一個 nil 檢查。 在這里,我還通過在跨度樣式中添加顏色選項來更改字體的顏色。 這是完全可選的。

SWIFT 4.2 的快速解決方案

    let fontName =  "PFHandbookPro-Regular"
    let fontSize = 20
    let fontSetting = "<span style=\"font-family: \(fontName);font-size: \(fontSize)\"</span>"
    webView.loadHTMLString( fontSetting + myHTMLString, baseURL: nil)

span 是一個內聯元素,這意味着它可以與其他元素在一條線上

UIWebview使用在 HTML 中設置的字體。 沒有“默認字體”。 即使沒有特別提及字體,您也無權設置它。 這一切都在我們無法訪問的 WebKit 中。

對於 Swift,我在webViewDidFinishLoad使用了這個

webView.stringByEvaluatingJavaScriptFromString("document.getElementsByTagName('body')[0].style.fontFamily =\"HelveticaNeue\"");
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
{
     webView.evaluateJavaScript("document.getElementsByTagName('body')[0].style.fontFamily =\"-apple-system\"")
     webView.evaluateJavaScript("document.getElementsByTagName('body')[0].style.fontSize = \"xx-large\"")
}

給熟悉 js 的人加分,把它折疊成一個單一的評估

在 wkwebview 中包裝在 span 標簽中的效果為零

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM