如何更改 WKWebView 或 UIWebView 默认字体

2023-10-22移动开发问题
9

本文介绍了如何更改 WKWebView 或 UIWebView 默认字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

UIWebViewWKWebView默认使用什么字体?我希望能够改变这一点.但我不想在 html 字符串中执行此操作,而是希望有类似的内容:

What font does UIWebView and WKWebView use 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:

// 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?

推荐答案

您可以使用您的 UIFont 对象(这样您可以更轻松地设置和修改它),但将 HTML 包装在 span 代替;font 标签已被弃用 自 HTML 4.01 起.

You can use your UIFont object (so you can set it and modify more easily), but wrap the HTML in a span instead; font tags have been deprecated since HTML 4.01.

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

假设您已经创建了 NSString *htmlString,您可以使用字体的属性:

Assuming you already have the NSString *htmlString created, you can use the font's properties:

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

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

Alternatively, you could just supply the values instead of using a UIFont object:

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

这篇关于如何更改 WKWebView 或 UIWebView 默认字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

Cocos2d - 如何检查不同层中对象之间的交集
Cocos2d - How to check for Intersection between objects in different layers(Cocos2d - 如何检查不同层中对象之间的交集)...
2024-08-12 移动开发问题
8

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

如何将 32 位 PNG 转换为 RGB565?
How to convert 32 bit PNG to RGB565?(如何将 32 位 PNG 转换为 RGB565?)...
2024-08-12 移动开发问题
21

正确的 cocos2d 场景重启?
Proper cocos2d scene restart?(正确的 cocos2d 场景重启?)...
2024-08-12 移动开发问题
7