Changing Editing Behavior

You can control or augment user editing by implementing a WebView editing delegate, an object that conforms to the WebEditingDelegate protocol. An editing delegate may receive should messages before or did messages after an editing action. Typically, you implement an editing delegate if you want to change the default editing behavior.

Should Methods

You implement should methods if you want to control user editing actions—these messages are initiated by a user action not by simply invoking WebView editing methods programmatically. The should methods are of the form webView:should... and may return YES to permit an action or NO to disallow an action. Optionally, the delegate can take an alternative action and return NO so the sender doesn’t take additional action.

For example, you can control the insertion of text by implementing the webView:shouldInsertText:replacingDOMRange:givenAction: method. In this example, the user may use the Shift key to alter an insertion:

- (BOOL)webView:(WebView *)webView shouldInsertText:(NSString *)text replacingDOMRange:(DOMRange *)range givenAction:(WebViewInsertAction)action
{
    if ([self shiftKeyIsDown]) {
        NSString *string = [NSString stringWithFormat:@"Big-%@", text];
        [webView replaceSelectionWithText:string];
        DOMRange *range = [webView selectedDOMRange];
        [range collapse:NO];
        [webView setSelectedDOMRange:range affinity:NSSelectionAffinityUpstream];
        return NO;
    }
    return YES;
}

Did Methods

A WebView editing delegate is automatically registered to receive notification of editing actions. The WebView editing delegate is sent a webViewDid... notification message—where sender is a WebView object—after the action takes place.