Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: customize the font size #268

Merged
merged 22 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
489af25
add change font size function
NeverAgain11 Nov 30, 2023
b628f53
remove debug log
NeverAgain11 Dec 18, 2023
ae4d9b1
Fix label font size hasn't been customised
NeverAgain11 Dec 19, 2023
d8605d3
perf: adjust the range of custom font sizes
tisfeng Dec 19, 2023
8c9de21
perf: add property fontSizeRatio
tisfeng Dec 19, 2023
b2d7271
perf: rename currentFontSizeRatio to fontSizeRatio
tisfeng Dec 19, 2023
292215f
fix: font for pos meaning do not need bold
tisfeng Dec 19, 2023
156bd28
fix: font for service name do not need to customzie
tisfeng Dec 19, 2023
bf7f1df
perf: update Localizable.xcstrings
tisfeng Dec 19, 2023
f864d5b
added drag gesture to customize the font size, fixed the missing buoy…
NeverAgain11 Dec 20, 2023
5b4466a
feat: make webView iframe font size follow user custom font size
tisfeng Dec 20, 2023
6fd7ae0
Merge branch 'dev' into changeFontSize
tisfeng Dec 20, 2023
0c5374d
Add keyboard shortcuts to customise font size
NeverAgain11 Dec 21, 2023
e623924
perf: adaptive big word title and details summary title font size
tisfeng Dec 21, 2023
53df35d
perf: rename Shortcut to Shortcuts
tisfeng Dec 21, 2023
24fd3a1
add the hints view for the keyboard shortcuts of change font size
NeverAgain11 Dec 22, 2023
e6e6458
perf: update Localizable.xcstrings
tisfeng Dec 22, 2023
2c9f85a
perf(UI): adjust change font size view UI
tisfeng Dec 22, 2023
caa6864
chore: add library Hue for color extension
tisfeng Dec 22, 2023
7059073
perf(UI): adjust change font size view color
tisfeng Dec 22, 2023
e57b6a1
perf(UI): adjust change font size view UI
tisfeng Dec 22, 2023
ad4af8f
Merge branch 'dev' into changeFontSize
tisfeng Dec 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: make webView iframe font size follow user custom font size
  • Loading branch information
tisfeng committed Dec 20, 2023
commit 5b4466af5229bc946fc34d9c31ea3a0043d8f6cf
8 changes: 4 additions & 4 deletions Easydict/Feature/Configuration/EZConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ - (void)setup {
self.fontSizes = @[@(1), @(1.1), @(1.2), @(1.3), @(1.4)];
[[NSUserDefaults standardUserDefaults]registerDefaults:@{kTranslationControllerFontKey: self.fontSizes.firstObject}];

self.fontSizeRatio = [[NSUserDefaults standardUserDefaults]floatForKey:kTranslationControllerFontKey];
self.fontSizeRatio = [[NSUserDefaults standardUserDefaults] floatForKey:kTranslationControllerFontKey];
}

#pragma mark - getter
Expand Down Expand Up @@ -418,9 +418,9 @@ - (void)setClearInput:(BOOL)clearInput {
[self logSettings:@{@"clear_input" : @(clearInput)}];
}

- (void)setFontSizeRatio:(CGFloat)currentFontSizeRatio {
_fontSizeRatio = currentFontSizeRatio;
[NSUserDefaults mm_write:@(currentFontSizeRatio) forKey:kTranslationControllerFontKey];
- (void)setFontSizeRatio:(CGFloat)fontSizeRatio {
_fontSizeRatio = fontSizeRatio;
[NSUserDefaults mm_write:@(fontSizeRatio) forKey:kTranslationControllerFontKey];
}

#pragma mark - Window Frame
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,17 @@
iframe.style.height = totalHeight + "px";

// console.log(`${i}: frame totalHeight: ${totalHeight}`);


var scrollHeight = getScrollHeight();

if (window.webkit && window.webkit.messageHandlers) {
// 调用 objc 方法
window.webkit.messageHandlers.objcHandler.postMessage({
method: "noteToUpdateScrollHeight",
scrollHeight: scrollHeight,
});
}
}
}

Expand Down Expand Up @@ -372,8 +383,10 @@
bigWordTitleHeight +
allIframesHeight +
dictionaryTitleHeight * iframesHeight.length;
console.log(`iframesHeight: [${iframesHeight}]`);
console.log(`scrollHeight: ${scrollHeight}`);

// console.log(`iframesHeight: [${iframesHeight}]`);
// console.log(`scrollHeight: ${scrollHeight}`);

return scrollHeight;
}

Expand Down Expand Up @@ -424,6 +437,19 @@
iframe.style.borderColor = newBorderColor;
}
}

// Change all iframes body font size
function changeIframeBodyFontSize(fontSize) {
console.log("fontSize: " + fontSize);
var iframes = document.getElementsByTagName('iframe');
for (var i = 0; i < iframes.length; i++) {
var iframe = iframes[i];
var frameDoc = iframe.contentDocument || iframe.contentWindow.document;

// 修改 iframe 中 body 的字体大小
frameDoc.body.style.fontSize = fontSize;
}
}

window.onload = function () {
// 重写 console.log,以便在 Xcode 中输出日志
Expand All @@ -442,35 +468,16 @@
};
}

if (isDarkMode()) {
// updateAllIframeColor(true);
// updateBackgroundColor(true);
}

updateAllIframeStyle();

var scrollHeight = getScrollHeight();

if (window.webkit && window.webkit.messageHandlers) {
// 调用 Objective-C 方法
window.webkit.messageHandlers.objcHandler.postMessage({
method: "getScrollHeight",
scrollHeight: scrollHeight,
});
}

var colorSchemeListener = window.matchMedia(
`(prefers-color-scheme: dark)`
);
colorSchemeListener.addEventListener(`change`, function (event) {
var isDarkMode = event.matches;
// updateAllIframeColor(isDarkMode);
// updateBackgroundColor(isDarkMode);
});
};
</script>
</head>

<body></body>

</html>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ NS_ASSUME_NONNULL_BEGIN

- (void)reset;

- (void)updateAllIframe;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "EZWebViewManager.h"
#import "EZConfiguration.h"

static NSString *kObjcHandler = @"objcHandler";
static NSString *kMethod = @"method";
Expand All @@ -27,30 +28,42 @@ - (WKWebView *)webView {
if (!_webView) {
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
[configuration.userContentController addScriptMessageHandler:self name:kObjcHandler];

_webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
}
return _webView;
}

#pragma mark - WKScriptMessageHandler

// 处理来自 JavaScript 的消息
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
id body = message.body;

if([message.name isEqualToString:kObjcHandler]) {
if([body[kMethod] isEqualToString:@"consoleLog"]) {
NSString *message = body[@"message"];
NSLog(@"<javascript log>: %@", message);
}

if([body[kMethod] isEqualToString:@"getScrollHeight"]) {
if([body[kMethod] isEqualToString:@"noteToUpdateScrollHeight"]) {
CGFloat scrollHeight = [body[@"scrollHeight"] floatValue];
if (self.didFinishUpdatingIframeHeightBlock) {
self.didFinishUpdatingIframeHeightBlock(scrollHeight);
}
}
}
}
}

#pragma mark - WebView evaluateJavaScript

- (void)updateAllIframe {
CGFloat fontSize = EZConfiguration.shared.fontSizeRatio * 100; // 140%
NSString *script = [NSString stringWithFormat:@"changeIframeBodyFontSize('%.1f%%'); updateAllIframeStyle();", fontSize];
[self.webView evaluateJavaScript:script completionHandler:^(id _Nullable result, NSError *_Nullable error) {
if (!error) {

}
}];
}

- (void)reset {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ - (void)refreshWithResult:(EZQueryResult *)result {
if (result.HTMLString.length) {
[self addSubview:self.webView];

if (result.webViewManager.isLoaded) {
[result.webViewManager updateAllIframe];
}

[result.webViewManager setDidFinishUpdatingIframeHeightBlock:^(CGFloat scrollHeight) {
mm_strongify(self);

Expand Down Expand Up @@ -1050,18 +1054,16 @@ - (BOOL)textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
NSLog(@"webView didFinishNavigation");

[self.result.webViewManager updateAllIframe];
}


- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
NSLog(@"didFailNavigation: %@", error);

}

/** 请求服务器发生错误 (如果是goBack时,当前页面也会回调这个方法,原因是NSURLErrorCancelled取消加载) */
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
NSLog(@"didFailProvisionalNavigation: %@", error);

}

// 监听 JavaScript 代码是否执行
Expand Down Expand Up @@ -1314,6 +1316,24 @@ - (void)getTextWithHref:(NSString *)href completionHandler:(void (^_Nullable)(NS
}];
}

- (void)updateWebViewAllIframeFontSize {
CGFloat fontSize = EZConfiguration.shared.fontSizeRatio * 100;

NSString *jsCode = [NSString stringWithFormat:
@"var iframes = document.querySelectorAll('iframe');"
@"for (var i = 0; i < iframes.length; i++) {"
@" var iframe = iframes[i];"
@" var frameDoc = iframe.contentDocument || iframe.contentWindow.document;"
@" frameDoc.body.style.fontSize = '%f%%';"
@"};", fontSize];

[self evaluateJavaScript:jsCode completionHandler:^(id _Nullable result, NSError *_Nullable error) {
if (!error) {

}
}];
}

#pragma mark -

// Convert text to multiple lines, such as "Hello world" to "Hello\nworld"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1257,19 +1257,19 @@ - (EZResultView *)resultCellAtRow:(NSInteger)row {
if ([service.serviceType isEqualToString:EZServiceTypeAppleDictionary]) {
EZAppleDictionary *appleDictService = (EZAppleDictionary *)service;

webView = result.webViewManager.webView;
EZWebViewManager *webViewManager = result.webViewManager;
webView = webViewManager.webView;
resultCell.wordResultView.webView = webView;

BOOL needLoadHTML = result.isShowing && result.HTMLString.length && !result.webViewManager.isLoaded;
BOOL needLoadHTML = result.isShowing && result.HTMLString.length && !webViewManager.isLoaded;
if (needLoadHTML) {
result.webViewManager.isLoaded = YES;
webViewManager.isLoaded = YES;

NSURL *htmlFileURL = [NSURL fileURLWithPath:appleDictService.htmlFilePath];
webView.navigationDelegate = resultCell.wordResultView;
[webView loadFileURL:htmlFileURL allowingReadAccessToURL:TTTDictionary.userDictionaryDirectoryURL];
} else if (result.webViewManager.needUpdateIframeHeight && result.webViewManager.isLoaded) {
NSString *script = @"updateAllIframeStyle();";
[webView evaluateJavaScript:script completionHandler:nil];
} else if (webViewManager.needUpdateIframeHeight && webViewManager.isLoaded) {
[webViewManager updateAllIframe];
}
}

Expand Down