1
votes

My OSX app embed a webview and display a custom toolbar for editing fonts/colors/font size etc. The webview loads an html and the user is able to edit it. My questions are:

  1. how to edit the text properties like font, color or size for the selected text?
  2. how to know the font's properties for the portion of text where the cursor laying? (i want change the information displayed in the toolbar)

I know it is possible because the webview has the right click menu with all the functionalities I'm looking for, but I want replicate this functionalities in my toolbar:

enter image description here

For test purpose I'm trying to set the font size in this way but nothing happens

[_contentWebView changeFont:[NSFont systemFontOfSize:32]];
[_contentWebView setNeedsDisplay:YES];

The apple documentation about editing function in the webview is really poor (creepy example) , someone can suggest me a tutorial, a book or something when find a rich documentation about the webview?

update:

I can retrive the selected HTML with this code:

-(NSString *)getSelectedHtml
{
    DOMRange *ff = [self selectedDOMRange];
    NSString *marki = [ff markupString];
    return marki;
}

but I DON'T want manipulate directly html... :(

1

1 Answers

2
votes

Now I can reply to my answer, the error is in the approach, the WebView is compliant with the NSText behaviour, so when the webview is the first responder you should use the standard approach for change fonts, color etc, for example:

Change the text allignement:

TextAlignementLeft:

[[self.view.window firstResponder] performSelector:@selector(alignLeft:) withObject:nil];

TextAlignementCenter:

[[self.view.window firstResponder] performSelector:@selector(alignCenter:) withObject:nil];

TextAlignementRight:

[[self.view.window firstResponder] performSelector:@selector(alignRight:) withObject:nil];

TextAlignementJustify:

[[self.view.window firstResponder] performSelector:@selector(alignJustified:) withObject:nil];

Add or remove font trait like bold and italic:

case ToolbarToolBold:
{
NSButton * dummyButton = [[NSButton alloc] init];
BOOL enabled = [(NSNumber*)value boolValue];
[dummyButton setTag:2];
if(enabled)
{
    [fontManager addFontTrait:dummyButton];
}
else
{
    [fontManager removeFontTrait:dummyButton];
}
}
break;
case ToolbarToolItalic:
{
NSButton * dummyButton = [[NSButton alloc] init];
BOOL enabled = [(NSNumber*)value boolValue];
[dummyButton setTag:1];
if(enabled)
{
    [fontManager addFontTrait:dummyButton];
}
else
{
    [fontManager removeFontTrait:dummyButton];
}
}
break;
case ToolbarToolUnderline:
{
[[self.view.window firstResponder] performSelector:@selector(underline:) withObject:nil];
}