cjwirth / RichEditorView

RichEditorView is a simple, modular, drop-in UIView subclass for Rich Text Editing.
BSD 3-Clause "New" or "Revised" License
1.9k stars 445 forks source link

Wrong attribute if cursor is in the beginning of word #154

Open andrey-lisovskiy opened 6 years ago

andrey-lisovskiy commented 6 years ago

First of all - thanks for your work! I've noticed the problem if you have such text: word bold word and then putting cursor at the beginning of bold word, bar is updating with bold state, but when you type any letter it is not bold, however, bar is still showing that Bold Formatting is enabled.

2018-02-14 16 22 54
andrey-lisovskiy commented 6 years ago

Solved with extension for NSAttributedString:

- (NSDictionary *)typingAttributesForRange:(NSRange)range
{
    NSInteger index = 0;

    if (range.length) {
        index = range.location;
    } else {
        if (range.location > 0) {
            index = range.location - 1;

            if ([[self string] characterAtIndex:index] == '\n') {
                index++;
            }
        }
    }

    if (index >= [self length]) {
        return nil;
    }

    NSMutableDictionary *attributes = [[self attributesAtIndex:index effectiveRange:NULL] mutableCopy];

    return attributes;
}
scubax07 commented 3 years ago

Hello @andrey-lisovskiy I am facing the similar issue in my Swift project. Could you please explain how you solved it? Does adding the extension do the trick or we have to do something else?