I want to build a wysiwyg editor as a custom component using Xamarin.Forms renderer. I found some solution which is a Xamarin.iOS project which does something I need:
https://github.com/XAM-Consulting/TEditor.
I followed mentioned example and implemented Wysiwyg renderer for Android but I have problems with iOS renderer.
This is my overriden method of ViewRenderer<WysiwygEditor, UIView>
:
protected override void OnElementChanged(ElementChangedEventArgs<WysiwygEditor> e)
{
base.OnElementChanged(e);
if (Control == null)
{
_view = new UIView();
_editorWebView = new UIWebView();
_view.Add(_editorWebView);
var interpretter = new JavaScriptEnterpretter(_editorWebView);
_jsApi = new RichEditorApi(interpretter);
_editorWebView.Delegate = new WysiwygWebViewClient(_jsApi, Element.Html);
_editorWebView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth
| UIViewAutoresizing.FlexibleHeight
| UIViewAutoresizing.FlexibleTopMargin
| UIViewAutoresizing.FlexibleBottomMargin;
_editorWebView.KeyboardDisplayRequiresUserAction = false;
_editorWebView.ScalesPageToFit = true;
_editorWebView.BackgroundColor = UIColor.White;
_editorWebView.ScrollView.Bounces = false;
_keyboardDidFrameToken = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.DidChangeFrameNotification, KeyboardDidFrame);
_keyboardWillShowToken = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, KeyboardWillShowOrHide);
_keyboardWillHideToken = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, KeyboardWillShowOrHide);
BuildToolbar(Element.Toolbars);
interpretter.Init();
_jsApi.SetPlatformAsIOS();
SetNativeControl(_view);
}
if (e.OldElement != null)
{
e.OldElement.Cleanup();
}
if (e.NewElement != null)
{
e.NewElement.Initialize(_jsApi);
SetHtml("some html");
}
}
WysiwygEditor is my class which inherits Xamarin.Forms.View.
The problem is that javascript calls are not working (such as _editorWebView.EvaluateJavascript("document.execCommand('bold', false, null)")
). HTML which is loaded into _editorWebView
contains a tag with contenteditable=true
attribute
So, my question is:
- How to move TEditorViewController from the example to viewrenderer correctly in my project?
- What is the aim of the property named ViewController in the ViewRenderer? Should I need to override it?