简体   繁体   English

如何更改 WKWebView 或 UIWebView 默认字体

[英]How to change WKWebView or UIWebView default font

What font does UIWebView and WKWebView use by default? UIWebViewWKWebView默认使用什么字体? I would like to be able to change that.我希望能够改变这一点。 But I don't want to do it in the html string, instead I want to have something like:但我不想在 html 字符串中执行此操作,而是希望使用以下内容:

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

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

is there such property or some other way?有这样的财产或其他方式吗?

You can use your UIFont object (so you can set it and modify more easily), but wrap the HTML in a span instead;你可以使用你的UIFont对象(所以你可以更容易地设置和修改它),但是将 HTML 包装在一个span font tagshave been deprecated since HTML 4.01 .自 HTML 4.01起不推荐使用font标签

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

Assuming you already have the NSString *htmlString created, you can use the font's properties:假设您已经创建了NSString *htmlString ,您可以使用字体的属性:

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

Alternatively, you could just supply the values instead of using a UIFont object:或者,您可以只提供值而不是使用UIFont对象:

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

Just prefix a <font face> tag to your string before loading into the webView.在加载到 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'"];

There is the swift 3 solution :有 swift 3 解决方案:

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

I have just put the default iOS font for this example我刚刚为这个例子添加了默认的 iOS 字体

Here is my easy-to-use and easy-to-expand solution with more font settings:这是我的易于使用且易于扩展的解决方案,具有更多字体设置:

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];

In case someone is looking for a solution in Swift 3, I used this (based on jterry's answer):如果有人在 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)

Be aware that here I'm not making sure the font isn't nil.请注意,在这里我不确保字体不为零。 A nil check could be added before loading the HTML string.在加载 HTML 字符串之前可以添加一个 nil 检查。 Here I'm also changing the color of the font by adding the color option in the style of the span.在这里,我还通过在跨度样式中添加颜色选项来更改字体的颜色。 This is completely optional.这是完全可选的。

Quick solution for SWIFT 4.2 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 is an inline element, meaning that it can be on a line with other elements span 是一个内联元素,这意味着它可以与其他元素在一条线上

UIWebview uses the font that's set in the HTML. UIWebview使用在 HTML 中设置的字体。 There is not 'default font'.没有“默认字体”。 Even if no font is specifically mentioned, you do not have access to set it.即使没有特别提及字体,您也无权设置它。 It's all inside the WebKit, which we don't have access to.这一切都在我们无法访问的 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\"")
}

bonus points to people familiar with js to fold this into a single evaluation给熟悉 js 的人加分,把它折叠成一个单一的评估

wrapping in span tag had zero effect in wkwebview在 wkwebview 中包装在 span 标签中的效果为零

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

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