ZSSRichTextEditor 是一款基于 webView 的富文本編輯器咪辱。由于項目需要,對ZSSRichTextEditor的UI進行了修改积仗,添加了標題俏蛮、輸入回調(diào)围橡、多圖上傳等蕾各。github:https://github.com/Ymuyangyin/ZWTextEditor
JS調(diào)用OC
通過WKWebViewDelegate代理方法,來監(jiān)聽輸入字數(shù)等方法
- (void)webView:(WKWebView*)webView decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler{
NSString *urlString = navigationAction.request.URL.absoluteString;
YPLog(@"decidePolicyForNavigationAction = %@",urlString);
[self.webView updatePlaceHolderStatu];
[self fontBarUpdateWithString:urlString];
[self handleWithString:urlString];
WeakSelf(self);
[webView getCaretYPositionPositionHandleCallback:^(id _Nullable obj, NSError * _Nullable error) {
CGFloat currentPos = [obj floatValue];
if (currentPos > SCREEN_HEIGHT - weakself.keboardHeight - 49) {
[weakself.webView autoScrollTop:currentPos+weakself.toolBarView.yp_height];
}
}];
decisionHandler(WKNavigationActionPolicyAllow); // 必須實現(xiàn) 加載
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
if ([YPStringUtils isNotEmpty:self.titles]) {
[webView setupTitle:self.titles];
}
if ([YPStringUtils isNotEmpty:self.content]) {
[webView setupContent:self.content];
[webView placeHolderWithHidden:YES];
}
}
OC調(diào)用JS
oc調(diào)用js是通過- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler
注入js代碼
具體的實現(xiàn)在WKWebView+ZWWKWebViewExtension
文件中
- (void)evaluateJavaScriptRelust:(NSString *)jsAction
Callback:(void (^)(id _Nullable obj, NSError * _Nullable error))callback{
dispatch_async(dispatch_get_main_queue(), ^{
[self evaluateJavaScript:jsAction completionHandler:^(id _Nullable obj, NSError * _Nullable error) {
YPLog(@"jsAction == %@, obj == %@ , ERROR == %@ ",jsAction,error,obj);
if (callback) {
callback(obj,error);
}
}];
});
}
新增標題和輸入字數(shù)監(jiān)聽
在RichEditor.html
文件中
<div id="title">
<textarea id="article_title" line-height="normal" style= "overflow:hidden; resize:none; " placeholder="請輸入標題" maxlength="30000" ></textarea>
</div>
<div id="title_tips">
<input id="article_title_tips" name="vj_article_title_tips" readonly="readonly" type="text" value="標題字數(shù)不可超過25字" style="border:none; display:none">
</div>
使用方法
通過繼承ZSSRichTextEditor
修復(fù)找不到 UIRemoteKeyboardWindow
/**
從給定的 window 里尋找代表鍵盤當前布局位置的 view糕档。
iOS 15 及以前(包括用 Xcode 13 編譯的 App 運行在 iOS 16 上的場景)碰纬,鍵盤的 UI 層級是:
|- UIApplication.windows
|- UIRemoteKeyboardWindow
|- UIInputSetContainerView
|- UIInputSetHostView - 鍵盤及 webView 里的輸入工具欄(上下鍵萍聊、Done鍵)
|- _UIKBCompatInputView - 鍵盤主體按鍵
|- TUISystemInputAssistantView - 鍵盤頂部的候選詞欄、emoji 鍵盤頂部的搜索框
|- _UIRemoteKeyboardPlaceholderView - webView 里的輸入工具欄的占位(實際的 view 在 UITextEffectsWindow 里)
iOS 16 及以后(僅限用 Xcode 14 及以上版本編譯的 App)悦析,UIApplication.windows 里已經(jīng)不存在 UIRemoteKeyboardWindow 了寿桨,所以退而求其次,我們通過 UITextEffectsWindow 里的 UIInputSetHostView 來獲取鍵盤的位置——這兩個 window 在布局層面可以理解為鏡像關(guān)系强戴。
|- UIApplication.windows
|- UITextEffectsWindow
|- UIInputSetContainerView
|- UIInputSetHostView - 鍵盤及 webView 里的輸入工具欄(上下鍵亭螟、Done鍵)
|- _UIRemoteKeyboardPlaceholderView - 整個鍵盤區(qū)域,包含頂部候選詞欄骑歹、emoji 鍵盤頂部搜索欄(有時候不一定存在)
|- UIWebFormAccessory - webView 里的輸入工具欄的占位
|- TUIInputAssistantHostView - 外接鍵盤時可能存在预烙,此時不一定有 placeholder
|- UIInputSetHostView - 可能存在多個,但只有一個里面有 _UIRemoteKeyboardPlaceholderView
所以只要找到 UIInputSetHostView 即可道媚,優(yōu)先從 UIRemoteKeyboardWindow 找扁掸,不存在的話則從 UITextEffectsWindow 找翘县。
*/
+ (UIView *)positionedKeyboardViewInWindow:(UIWindow *)window {
if (!window) return nil;
NSString *windowName = NSStringFromClass(window.class);
if ([windowName isEqualToString:@"UIRemoteKeyboardWindow"]) {
UIView *result = [[window.subviews qmui_firstMatchWithBlock:^BOOL(__kindof UIView * _Nonnull subview) {
return [NSStringFromClass(subview.class) isEqualToString:@"UIInputSetContainerView"];
}].subviews qmui_firstMatchWithBlock:^BOOL(__kindof UIView * _Nonnull subview) {
return [NSStringFromClass(subview.class) isEqualToString:@"UIInputSetHostView"];
}];
return result;
}
if ([windowName isEqualToString:@"UITextEffectsWindow"]) {
UIView *result = [[window.subviews qmui_firstMatchWithBlock:^BOOL(__kindof UIView * _Nonnull subview) {
return [NSStringFromClass(subview.class) isEqualToString:@"UIInputSetContainerView"];
}].subviews qmui_firstMatchWithBlock:^BOOL(__kindof UIView * _Nonnull subview) {
return [NSStringFromClass(subview.class) isEqualToString:@"UIInputSetHostView"] && subview.subviews.count;
}];
return result;
}
return nil;
}