ios 如何更改 UIWebView 或 WKWebView 默认字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12533445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:18:00  来源:igfitidea点击:

How to change UIWebView or WKWebView default font

iosobjective-cswiftuiwebviewwkwebview

提问by Au Ris

What font does UIWebViewand WKWebViewuse by default? 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:

什么字体确实UIWebViewWKWebView默认情况下使用?我希望能够改变这一点。但我不想在 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?

有这样的财产或其他方式吗?

回答by jterry

You can use your UIFontobject (so you can set it and modify more easily), but wrap the HTML in a spaninstead; fonttags have been deprecatedsince HTML 4.01.

您可以使用您的UIFont对象(因此您可以更轻松地设置和修改它),但将 HTML 包装在一个中span自 HTML 4.01起不推荐使用font标签

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

Assuming you already have the NSString *htmlStringcreated, 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 UIFontobject:

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

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

回答by TijuanaKez

Just prefix a <font face>tag to your string before loading into the webView.

<font face>在加载到 webView 之前,只需为您的字符串添加一个标签。

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

回答by William Niu

You could try to set the font by injecting a line of JavaScript like this:

您可以尝试通过注入一行 JavaScript 来设置字体,如下所示:

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

回答by Kevin ABRIOUX

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 字体

回答by Iris

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. 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.

请注意,在这里我不确保字体不为零。在加载 HTML 字符串之前可以添加一个 nil 检查。在这里,我还通过在跨度样式中添加颜色选项来更改字体的颜色。这是完全可选的。

回答by Al-Noor Ladhani

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

回答by Ravi

UIWebviewuses the font that's set in the 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.

UIWebview使用在 HTML 中设置的字体。没有“默认字体”。即使没有特别提及字体,您也无权设置它。这一切都在我们无法访问的 WebKit 中。

回答by alitosuner

For Swift, I use this one in webViewDidFinishLoad

对于 Swift,我在 webViewDidFinishLoad

webView.stringByEvaluatingJavaScriptFromString("document.getElementsByTagName('body')[0].style.fontFamily =\"HelveticaNeue\"");

回答by Hyman

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 是一个内联元素,这意味着它可以与其他元素在一条线上