Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Changing font stuff in NSTextView + Undo



On Friday, January 30, 2004, at 07:51 PM, Douglas Davidson wrote:

Manipulate the text storage directly. Iterate over it by effective ranges for NSFontAttributeName, making your changes. Be sure to call -[NSTextView shouldChangeTextInRange:replacementString:] first, then -[NSTextStorage beginEditing], then make your changes, then call -[NSTextStorage endEditing] and -[NSTextView didChangeText].

The basics of what I have so far is the following pseudo-code:
range = [NSTextView rangeForUserCharacterAttributeChange];
if (range.location == NSNotFound) return;
if (![NSTextView shouldChangeTextInRange:range replacementString:nil]) return;
if (![NSTextView isRichText]) {
oldFont = [NSTextView font];
newFont = MyConvertFont(oldFont);
[NSTextView setFont:newFont];
} else if (range.length == 0) {
oldAttrs = [NSTextView typingAttributes];
oldFont = [oldAttrs objectForKey:NSFontAttributeName];
newFont = MyConvertFont(oldFont);
newAttrs = [NSMutableDictionary dictionaryWithDictionary:oldAttrs];
[newAttrs setObject:newFont forKey:NSFontAttributeName];
[NSTextView setTypingAttributes:newAttrs];
} else {
attrString = [NSTextView attributedSubstringFromRange:range];
while (not all attrString scanned) {
oldFont = [attrString attribute:NSFontAttributeName...];
newFont = MyConvertFont(oldFont);
[NSTextView setFont:newFont range:...];
}
}
[NSTextView didChangeText];
[[NSTextView undoManager] setActionName:@"Set Font"];

If I understand you and Apple's docs correctly, manipulating the NSTextStorage directly would be more efficient. Let me guess, the non-rich text and the insertion point parts are OK; but, the selection part is not good because each time I call [NSTextView setFont:newFont range:...] the NSTextView calls [NSTextStorage beginEditing] and [NSTextStorage endEditing]? And it would be better for me to make all the changes to the NSTextStorage at once?

Thank you,
Bernie
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: Changing font stuff in NSTextView + Undo (From: Douglas Davidson <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.