ZSSRichTextEditor Documentation

repository·master·Indexed 26 days ago

https://github.com/nnhubbard/zssrichtexteditor

A WYSIWYG rich text editor for iOS (iOS 7+) supporting standard text formatting, source view with syntax highlighting, and custom extensions. Features include HTML content manipulation, custom toolbar buttons, CSS injection, and event handling for editor changes, hashtags, and mentions.

Tokens
1.6K
Snippets
7
Records
9
Agent score
37%

What's inside ZSSRichTextEditor

  1. Initialize and Configure ZSSRichTextEditor

    master

    To use the editor, subclass ZSSRichTextEditor and configure its properties. You can set the base URL for relative links (like images), enable HTML pretty-printing in the source view, and set the initial HTML content.

    Key properties:

    • baseURL: An NSURL used for resolving relative links.
    • formatHTML: A boolean to enable/disable pretty-printing in the source view.
    • toolbarItemTintColor: Sets the color of the toolbar buttons.
    • toolbarItemSelectedTintColor: Sets the color of the toolbar buttons when selected.
    • enabledToolbarItems: An array of ZSSRichTextEditorToolbar... constants to limit visible buttons.
    • alwaysShowToolbar: A boolean to keep the toolbar visible even when the keyboard is hidden.
    ```objective-c
    // HTML Content to set in the editor
    NSString *html = @"<!-- This is an HTML comment -->"
    "<p>This is a test of the <strong>ZSSRichTextEditor</strong> by <a title=\"Zed Said\" href=\"http://www.zedsaid.com\">Zed Said Studio</a></p>";
    
    // Set the base URL if you would like to use relative links, such as to images.
    self.baseURL = [NSURL URLWithString:@
  2. Install ZSSRichTextEditor

    master

    You can install ZSSRichTextEditor using CocoaPods or by manually adding the source to your project.

    Requirements:

    • iOS 7 or later
    • CoreGraphics.framework
    • CoreText.framework

    Manual Installation Steps:

    1. Copy the Source folder to your project.
    2. Subclass ZSSRichTextEditor.

    CRITICAL XCode Configuration: When adding the files manually, XCode may automatically add ZSSRichTextEditor.js to your 'Compile Sources' build phase. This will prevent the editor from working correctly. You must remove ZSSRichTextEditor.js from 'Compile Sources' and add it to 'Copy Bundle Resources' instead.

  3. Handle Editor Change Events

    master

    To listen for changes in the editor content, set receiveEditorDidChangeEvents = YES; in your viewDidLoad. You can then implement the editorDidChangeWithText:andHTML: method to receive updates for both plain text and HTML.

    // In viewDidLoad
    self.receiveEditorDidChangeEvents = YES;
    
    // Implementation
    - (void)editorDidChangeWithText:(NSString *)text andHTML:(NSString *)html {
        NSLog(@"Text Has Changed: %@", text);
        NSLog(@"HTML Has Changed: %@", html);
    }
  4. Add Custom Toolbar Buttons

    master

    You can extend the toolbar by adding custom UIButton instances. Use addCustomToolbarItemWithButton: to register the button.

    ```objective-c
    UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, buttonWidth, 28.0f)];
    [myButton setTitle:@
  5. Implement Custom Pickers for Links and Images

    master

    If you want to use an alternate method for selecting images or links (e.g., picking from the camera roll), override the following methods in your ZSSRichTextEditor subclass. When the user taps the alternate picker icon (crosshair) in the toolbar, these methods are called.

    - (void)showInsertURLAlternatePicker {
        [self dismissAlertView];
        // Show your custom picker
    }
    
    - (void)showInsertImageAlternatePicker {
        [self dismissAlertView];
        // Show your custom picker
    }
  6. Manipulate HTML Content

    master

    Use the following methods to interact with the editor's content:

    • setHTML:: Sets the initial HTML content.
    • getHTML: Returns the current content as an NSString.
    • insertHTML:: Inserts a specific HTML string at the current caret position.
    • setPlaceholder:: Sets a placeholder string to display when there is no content.
    ```objective-c
    // Set the initial HTML for the editor
    [self setHTML:html];
    
    // Returns an NSString
    [self getHTML];
    
    // Insert HTML at the current caret position
    NSString *html = @"<strong>I love cats!</strong>";
    [self insertHTML:html];
    
    // Set A Placeholder
    [self setPlaceholder:@
  7. Handle Hashtag and Mention Events

    master

    The editor can recognize hashtags and mentions. Override these methods in your subclass to respond to them:

    • hashtagRecognizedWithWord:: Called when a hashtag is identified.
    • mentionRecognizedWithWord:: Called when a mention is identified.
    - (void)hashtagRecognizedWithWord:(NSString *)word {
        NSLog(@"Hashtag has been recognized: %@", word);
    }
    
    - (void)mentionRecognizedWithWord:(NSString *)word {
        NSLog(@"Mention has been recognized: %@", word);
    }