How to change WKWebView or UIWebView default font(如何更改 WKWebView 或 UIWebView 默认字体)
问题描述
UIWebView和WKWebView默认使用什么字体?我希望能够改变这一点.但我不想在 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 默认字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何更改 WKWebView 或 UIWebView 默认字体
基础教程推荐
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
