Hello,
I try to append a string to the existing content of a text view.
And everything goes to pieces, so:
What am I doing wrong? Just before setting the string, a log shows that it is correct.
Thanks.
Hello,
I try to append a string to the existing content of a text view.
And everything goes to pieces, so:
What am I doing wrong? Just before setting the string, a log shows that it is correct.
Thanks.
Hi,
the class of the content of a text view is actually MSMutableAttributedString, so try this
NSAttributedString *attributedString;
attributedString = [[NSAttributedString alloc] initWithString:finalInsert]];
[[mainTextView textStorage] appendAttributedString:attributedString];
[attributedString release];
Hi Stefan,
This time I get this:
Regards,
There must be another problem with glyphs.
The code in my post is the standard method to append a string to the contents of a text view
Have you previously invoked beginEditing without endEditing?
Hello Shane,
No, neither beginEditing nor endEditing.
I really don’t understand. When I log the previous text + the inserted string, everything looks fine. When I try to set the new text, I get this error.
The attributed string is bound to a attribute (property) of a Core Data entity. When I edit it in the text view, I can copy/paste, type, insert images and so on: no problem to edit, save and retrieve. Is it something wrong that the console doesn’t reveal? How could I find it?
I suspect the fact it’s not working has something to do with the way Core Data works. Can you use insertText: instead?
This time I get:
2012-09-10 09:56:21.168 AutoText[66674:303] *** -[NSConcreteTextStorage attributesAtIndex:effectiveRange:]: Range or index out of bounds
If I set the selection to the very beginning of the text with
[mainTextView setSelectedRange: NSMakeRange(0,0)];
I get:
2012-09-10 09:58:36.083 AutoText[66710:303] Ignoring exception raised in __block_global_13: *** -[NSConcreteTextStorage attributesAtIndex:effectiveRange:]: Range or index out of bounds
2012-09-10 09:58:42.958 AutoText[66710:303] An uncaught exception was raised
2012-09-10 09:58:42.959 AutoText[66710:303] *** -[NSConcreteTextStorage attribute:atIndex:longestEffectiveRange:inRange:]: Range or index out of bounds
2012-09-10 09:58:43.005 AutoText[66710:303] (
0 CoreFoundation 0x00007fff8a2780c6 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff930763f0 objc_exception_throw + 43
2 CoreFoundation 0x00007fff8a277e9c +[NSException raise:format:] + 204
3 AppKit 0x00007fff936ea875 -[NSConcreteTextStorage attribute:atIndex:longestEffectiveRange:inRange:] + 132
4 AppKit 0x00007fff93666fda -[NSTextView updateFontPanel] + 885
Well, once again, the solution that I found isn’t simple, but it works and even avoid the modification of the original (i.e. the Entity Attribute), which was another goal (I formerly discarded the changes before re-opening the file ):
So the text is re-initialized at each modification. Looking back, it was not SO complicated.
Thanks to both of you!
By the way, do you know how to insert a string into a text using the current text’s font? the equivalent of the Pages’s “Paste and Apply Style”. my insertion uses the default font (Helvetica 12, which is ugly enough).
EDIT: Found. InsertText does the job. IF the textView is editable. I like the docs. It’s like searching Easter eggs