OC 技术 WKWebView修改字体

一直觉得自己写的不是技术,而是情怀,一个个的教程是自己这一路走来的痕迹。靠专业技能的成功是最具可复制性的,希望我的这条路能让你们少走弯路,希望我能帮你们抹去知识的蒙尘,希望我能帮你们理清知识的脉络,希望未来技术之巅上有你们也有我。

OC 技术 WKWebView修改字体_第1张图片

前言

正题

方法1:

OC 技术 WKWebView修改字体_第2张图片

#import "ViewController.h"
#import "Masonry.h"
#import <WebKit/WebKit.h>

@interface ViewController ()
@property (nonatomic,strong) WKWebView *webView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.webView = [WKWebView new];
    
    [self.view addSubview:self.webView];
    self.webView.makeCons(^{
      make.left.right.equal.view(self.view);
      make.top.equal.view(self.view);
      make.bottom.equal.view(self.view).constants(0);
    });

    //本地加载方式一:
    NSString *baseURL = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"html"];
    NSString *text1 =  @"

The Polski Chór Kameralny Schola Cantorum Gedanensis is a full-time professional chamber choir. It was founded in Gdańsk in 1978 by Ireneusz Łukaszewski, and since 1983 has been under the direction of his brother, Jan Łukaszewski.

The choir specialises in variety, performing music from all genres and periods, including Gregorian chant, motets, opera, oratorio, film music, jazz, pop, and modern classical compositions. It has given 620 world premiere performances by composers including Henryk Mikołaj Górecki, Krzysztof Penderecki, Wojciech Kilar, Augustyn Bloch, Krzysztof Meyer, Andrzej Koszewski and Paweł Łukaszewski, and many more who have dedicated compositions to the choir.

Polski Chór Kameralny is a frequent guest at renowned international festivals including the Warsaw Autumn, the Ruhr Festival and Tenso Days in Berlin, and it has also released numerous albums and made recordings for broadcast in Poland, Europe and North America, many of which have won prizes such as the Fryderyk Award and the Orphée d’Or. Polski Chór Kameralny prepares around 40 different programmes a year and co-operates with orchestras including the Polish National Radio Symphony Orchestra, the Polish Baltic Philharmonic, the Polish Chamber Philharmonic in Sopot, Sinfonia Varsovia, the Academy of Ancient Music, the Venice Baroque Orchestra, the Akademie für Alte Musik Berlin, and many more, as well as with choral conductors including Eric Ericson, Uwe Gronostay, Frieder Bernius and Stephen Layton, among others.

Polski Chór Kameralny also organises its own music festivals, most notably the annual ‘Mozartiana’ International Festival presenting Mozart’s music in various guises, drawing thousands of visitors to the historic city centre of Gdańsk every year. Polski Chór Kameralny is a member of the Tenso European Network, and an institution of culture of the city of Gdańsk.

\"Polish
Photo: Michał Mazurkiewicz

"
; //方法一 UIFont *font = [UIFont systemFontOfSize:40];//系统默认字体 UIFont *font1 = [UIFont fontWithName:@"Zapfino" size:14];//系统提的字体。 UIFont *font2 = [UIFont fontWithName:@"FiraSans-Black" size:40];//倒入的字体无法修改 NSString *fontColor =@"FF0000"; NSString *htmlString1 =[NSString stringWithFormat:@" \n" " \n" " \n" " \n" "%@ \n" "", font1.familyName,fontColor,font1,text1]; [self.webView loadHTMLString:htmlString1 baseURL:[NSURL fileURLWithPath:baseURL]]; } @end

方法2:

在这里插入图片描述

#import "ViewController.h"
#import "Masonry.h"
#import <WebKit/WebKit.h>

@interface ViewController ()
@property (nonatomic,strong) WKWebView *webView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.webView = [WKWebView new];
    
    [self.view addSubview:self.webView];
    self.webView.makeCons(^{
      make.left.right.equal.view(self.view);
      make.top.equal.view(self.view);
      make.bottom.equal.view(self.view).constants(0);
    });

    //本地加载方式一:
    NSString *baseURL = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"html"];
    NSString *text1 =  @"

The Polski Chór Kameralny Schola Cantorum Gedanensis is a full-time professional chamber choir. It was founded in Gdańsk in 1978 by Ireneusz Łukaszewski, and since 1983 has been under the direction of his brother, Jan Łukaszewski.

The choir specialises in variety, performing music from all genres and periods, including Gregorian chant, motets, opera, oratorio, film music, jazz, pop, and modern classical compositions. It has given 620 world premiere performances by composers including Henryk Mikołaj Górecki, Krzysztof Penderecki, Wojciech Kilar, Augustyn Bloch, Krzysztof Meyer, Andrzej Koszewski and Paweł Łukaszewski, and many more who have dedicated compositions to the choir.

Polski Chór Kameralny is a frequent guest at renowned international festivals including the Warsaw Autumn, the Ruhr Festival and Tenso Days in Berlin, and it has also released numerous albums and made recordings for broadcast in Poland, Europe and North America, many of which have won prizes such as the Fryderyk Award and the Orphée d’Or. Polski Chór Kameralny prepares around 40 different programmes a year and co-operates with orchestras including the Polish National Radio Symphony Orchestra, the Polish Baltic Philharmonic, the Polish Chamber Philharmonic in Sopot, Sinfonia Varsovia, the Academy of Ancient Music, the Venice Baroque Orchestra, the Akademie für Alte Musik Berlin, and many more, as well as with choral conductors including Eric Ericson, Uwe Gronostay, Frieder Bernius and Stephen Layton, among others.

Polski Chór Kameralny also organises its own music festivals, most notably the annual ‘Mozartiana’ International Festival presenting Mozart’s music in various guises, drawing thousands of visitors to the historic city centre of Gdańsk every year. Polski Chór Kameralny is a member of the Tenso European Network, and an institution of culture of the city of Gdańsk.

\"Polish
Photo: Michał Mazurkiewicz

"
; //方法二 UIFont *font3 = [UIFont fontWithName:@"Zapfino" size:40];//系统提的字体。 NSString* htmlString2 = [NSString stringWithFormat:@"%@", font3.fontName, (int) font3.pointSize, text1]; [self.webView loadHTMLString:htmlString2 baseURL:[NSURL fileURLWithPath:baseURL]]; } @end

方法3:

在这里插入图片描述

#import "ViewController.h"
#import "Masonry.h"
#import <WebKit/WebKit.h>

@interface ViewController ()
@property (nonatomic,strong) WKWebView *webView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.webView = [WKWebView new];
    
    [self.view addSubview:self.webView];
    self.webView.makeCons(^{
      make.left.right.equal.view(self.view);
      make.top.equal.view(self.view);
      make.bottom.equal.view(self.view).constants(0);
    });

    //本地加载方式一:
    NSString *baseURL = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"html"];
    NSString *text1 =  @"

The Polski Chór Kameralny Schola Cantorum Gedanensis is a full-time professional chamber choir. It was founded in Gdańsk in 1978 by Ireneusz Łukaszewski, and since 1983 has been under the direction of his brother, Jan Łukaszewski.

The choir specialises in variety, performing music from all genres and periods, including Gregorian chant, motets, opera, oratorio, film music, jazz, pop, and modern classical compositions. It has given 620 world premiere performances by composers including Henryk Mikołaj Górecki, Krzysztof Penderecki, Wojciech Kilar, Augustyn Bloch, Krzysztof Meyer, Andrzej Koszewski and Paweł Łukaszewski, and many more who have dedicated compositions to the choir.

Polski Chór Kameralny is a frequent guest at renowned international festivals including the Warsaw Autumn, the Ruhr Festival and Tenso Days in Berlin, and it has also released numerous albums and made recordings for broadcast in Poland, Europe and North America, many of which have won prizes such as the Fryderyk Award and the Orphée d’Or. Polski Chór Kameralny prepares around 40 different programmes a year and co-operates with orchestras including the Polish National Radio Symphony Orchestra, the Polish Baltic Philharmonic, the Polish Chamber Philharmonic in Sopot, Sinfonia Varsovia, the Academy of Ancient Music, the Venice Baroque Orchestra, the Akademie für Alte Musik Berlin, and many more, as well as with choral conductors including Eric Ericson, Uwe Gronostay, Frieder Bernius and Stephen Layton, among others.

Polski Chór Kameralny also organises its own music festivals, most notably the annual ‘Mozartiana’ International Festival presenting Mozart’s music in various guises, drawing thousands of visitors to the historic city centre of Gdańsk every year. Polski Chór Kameralny is a member of the Tenso European Network, and an institution of culture of the city of Gdańsk.

\"Polish
Photo: Michał Mazurkiewicz

"
; //方法三 UIFont *font4 = [UIFont fontWithName:@"Zapfino" size:40];//系统提的字体。 NSString *htmlString3 = [NSString stringWithFormat:@"%@", font4.fontName,text1]; [self.webView loadHTMLString:htmlString3 baseURL:[NSURL fileURLWithPath:baseURL]]; } @end

方法4:

在这里插入图片描述

OC

#import "ViewController.h"
#import "Masonry.h"
#import <WebKit/WebKit.h>

@interface ViewController ()<WKNavigationDelegate>
@property (nonatomic,strong) WKWebView *webView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.webView = [WKWebView new];
    self.webView.navigationDelegate = self;
    [self.view addSubview:self.webView];
    self.webView.makeCons(^{
      make.left.right.equal.view(self.view);
      make.top.equal.view(self.view);
      make.bottom.equal.view(self.view).constants(0);
    });

    //本地加载方式一:
    NSString *baseURL = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"html"];
    NSString *text1 =  @"

The Polski Chór Kameralny Schola Cantorum Gedanensis is a full-time professional chamber choir. It was founded in Gdańsk in 1978 by Ireneusz Łukaszewski, and since 1983 has been under the direction of his brother, Jan Łukaszewski.

The choir specialises in variety, performing music from all genres and periods, including Gregorian chant, motets, opera, oratorio, film music, jazz, pop, and modern classical compositions. It has given 620 world premiere performances by composers including Henryk Mikołaj Górecki, Krzysztof Penderecki, Wojciech Kilar, Augustyn Bloch, Krzysztof Meyer, Andrzej Koszewski and Paweł Łukaszewski, and many more who have dedicated compositions to the choir.

Polski Chór Kameralny is a frequent guest at renowned international festivals including the Warsaw Autumn, the Ruhr Festival and Tenso Days in Berlin, and it has also released numerous albums and made recordings for broadcast in Poland, Europe and North America, many of which have won prizes such as the Fryderyk Award and the Orphée d’Or. Polski Chór Kameralny prepares around 40 different programmes a year and co-operates with orchestras including the Polish National Radio Symphony Orchestra, the Polish Baltic Philharmonic, the Polish Chamber Philharmonic in Sopot, Sinfonia Varsovia, the Academy of Ancient Music, the Venice Baroque Orchestra, the Akademie für Alte Musik Berlin, and many more, as well as with choral conductors including Eric Ericson, Uwe Gronostay, Frieder Bernius and Stephen Layton, among others.

Polski Chór Kameralny also organises its own music festivals, most notably the annual ‘Mozartiana’ International Festival presenting Mozart’s music in various guises, drawing thousands of visitors to the historic city centre of Gdańsk every year. Polski Chór Kameralny is a member of the Tenso European Network, and an institution of culture of the city of Gdańsk.

\"Polish
Photo: Michał Mazurkiewicz

"
; [self.webView loadHTMLString:text1 baseURL:[NSURL fileURLWithPath:baseURL]]; } // 加载完毕 - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation { UIFont *font4 = [UIFont fontWithName:@"Zapfino" size:40];//系统提的字体。 NSString *script = [NSString stringWithFormat:@"document.body.style.fontFamily = '%@'",font4.familyName]; [webView evaluateJavaScript:script completionHandler:nil]; } @end

Swift

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {   
    let font = UIFont.init(name: "PingFangSC-Medium", size: 11)
    let fontStyle = NSString.init(format: "document.body.style.fontFamily = '%@'",font!.fontName)
    webView.evaluateJavaScript(fontStyle as String)
}

你可能感兴趣的:(ios,swift,开发语言)