RichTextEditor
iOS 13 之后不再支持 UIWebView,所以將之前一篇文章iOS 富文本編輯器-UIWebView中 UIWebView 替換為 WKWebView腌乡。以下為更換到 WKWebView 的一些知識點(diǎn)铭腕。
富文本編輯器設(shè)計的知識點(diǎn)
Objective-C 語言調(diào)用 JavaScript 語言
通過WKWebView的
- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler;
方法來實(shí)現(xiàn)的图云。這樣我們就可以實(shí)現(xiàn)文字的加粗、下劃線、斜體等等一切我們想要的樣式刺洒。
詳見項目中 WKWebView+VJJSTool 文件老翘。
JavaScript 語言調(diào)用 Objective-C 語言
第一步:我們需要在WKWebView創(chuàng)建的過程中初始化添加ScriptMessageHandler
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
WKUserContentController *userController = [[WKUserContentController alloc] init];
configuration.userContentController = userController;
_mainWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, KMainWidth, KMainHeight) configuration:configuration];
[userController addScriptMessageHandler:self name:@"currentCookies"];
然后實(shí)現(xiàn)代理方法.監(jiān)聽JS的回調(diào).
//JS調(diào)用的OC回調(diào)方法
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
if ([message.name isEqualToString:@"jsMethod"]) {
//
}
}
然后在Html文件中添加點(diǎn)擊方法.
<div id="button_div" onclick="buttonDivAction()">
點(diǎn)擊調(diào)用OC方法
</div>
緊接著.在js文件中實(shí)現(xiàn)點(diǎn)擊事件.這時候要注意的是ScriptMessageHandler的名稱要與上面定義的一致.
function buttonDivAction() {
window.webkit.messageHandlers. jsMethod.postMessage({
"body": "buttonActionMessage"
});
}
輸入文字時缅叠,插入符號位置計算
ZSSRichTextEditor.js
zss_editor.calculateEditorHeightWithCaretPosition = function() {}
新增標(biāo)題與副標(biāo)題
editor.html
<div id="vj_title">
<input id="vj_article_title" maxlength="70" type="text" placeholder="請輸入標(biāo)題">
</div>
<div id="vj_abstract-title">
<input id="vj_article_abstract" maxlength="35" type="text" placeholder="請輸入文章摘要">
</div>
去掉鍵盤自帶的工具條
WKWebView+HackishAccessoryHiding.h
@property (nonatomic, assign) BOOL hidesInputAccessoryView;
自動彈出鍵盤
實(shí)現(xiàn)和 UIWebView 中 keyboardDisplayRequiresUserAction 相同功能。即在 web 頁面 focus 獲得焦點(diǎn)狀態(tài)下彈出鍵盤臀脏,UIWebView 中 keyboardDisplayRequiresUserAction 設(shè)置為 NO 就可以實(shí)現(xiàn)劝堪,但是WKWebView 中沒有此屬性,可以通過以下方式實(shí)現(xiàn):
WKWebView+HackishAccessoryHiding.m
-(void)allowDisplayingKeyboardWithoutUserAction {
Class class = NSClassFromString(@"WKContentView");
NSOperatingSystemVersion iOS_11_3_0 = (NSOperatingSystemVersion){11, 3, 0};
NSOperatingSystemVersion iOS_12_2_0 = (NSOperatingSystemVersion){12, 2, 0};
NSOperatingSystemVersion iOS_13_0_0 = (NSOperatingSystemVersion){13, 0, 0};
char * methodSignature = "_startAssistingNode:userIsInteracting:blurPreviousNode:changingActivityState:userObject:";
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_13_0_0]) {
methodSignature = "_elementDidFocus:userIsInteracting:blurPreviousNode:activityStateChanges:userObject:";
} else if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_12_2_0]) {
methodSignature = "_elementDidFocus:userIsInteracting:blurPreviousNode:changingActivityState:userObject:";
}
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_11_3_0]) {
SEL selector = sel_getUid(methodSignature);
Method method = class_getInstanceMethod(class, selector);
IMP original = method_getImplementation(method);
IMP override = imp_implementationWithBlock(^void(id me, void* arg0, BOOL arg1, BOOL arg2, BOOL arg3, id arg4) {
((void (*)(id, SEL, void*, BOOL, BOOL, BOOL, id))original)(me, selector, arg0, TRUE, arg2, arg3, arg4);
});
method_setImplementation(method, override);
} else {
SEL selector = sel_getUid("_startAssistingNode:userIsInteracting:blurPreviousNode:userObject:");
Method method = class_getInstanceMethod(class, selector);
IMP original = method_getImplementation(method);
IMP override = imp_implementationWithBlock(^void(id me, void* arg0, BOOL arg1, BOOL arg2, id arg3) {
((void (*)(id, SEL, void*, BOOL, BOOL, id))original)(me, selector, arg0, TRUE, arg2, arg3);
});
method_setImplementation(method, override);
}
}
詳細(xì)代碼請看項目 https://github.com/Vergil-wj/RichTextEditor
參考資料: