• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Changing font stuff in NSTextView + Undo
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Changing font stuff in NSTextView + Undo


  • Subject: Re: Changing font stuff in NSTextView + Undo
  • From: Bernie Zenis <email@hidden>
  • Date: Sat, 31 Jan 2004 14:50:44 -0500

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>)

  • Prev by Date: Re: Porting .NET/XML app to the Mac
  • Next by Date: Re: Setting textfields tab order programmatically
  • Previous by thread: Re: Changing font stuff in NSTextView + Undo
  • Next by thread: MVC and multiple tables
  • Index(es):
    • Date
    • Thread