FROM : Douglas Davidson
DATE : Mon Nov 01 18:39:11 2004
On Nov 1, 2004, at 8:47 AM, Matt Neuburg wrote:
>> Hi I am using cocoa, obj-c, and I want to change the font size of my
>> NSTextView with a slider, like Font Book does. I don't need help with
>> the slider part of it, just a basic way on how to change the font size
>> of the contents of a NSTextView, using code, not a font panel.
>
> With an NSTextView, you have to adjust the font for every style run
> individually. Luckily in Panther this is really easy: call
> changeAttributes
> and implement convertAttributes. Your convertAttributes will
> automatically
> be called for each style run, so you can set the font size and return
> the
> adjusted attributes dictionary. m.
There is also a superclass method setFont:, which will set the font for
the entire text. Be aware, though, that this does not include the
calls to shouldChangeTextInRange:replacementString:/didChangeText that
should be used for changes that are intended to respond to user
actions, so you would need to do those yourself, if for example you
wanted these changes to be undoable. Maybe something like this:
unsigned textLength = [[textView textStorage] length];
if (textLength > 0 && [textView
shouldChangeTextInRange:NSMakeRange(0, textLength)
replacementString:nil]) {
[textView setFont:newFont];
[textView didChangeText];
} else if (textLength == 0) {
[textView setFont:newFont]; // when text is empty, this just
changes the typing attributes
}
For a non-editable textview, such as e.g. FontBook usually uses,
setFont: by itself would be fine.
Douglas Davidson
DATE : Mon Nov 01 18:39:11 2004
On Nov 1, 2004, at 8:47 AM, Matt Neuburg wrote:
>> Hi I am using cocoa, obj-c, and I want to change the font size of my
>> NSTextView with a slider, like Font Book does. I don't need help with
>> the slider part of it, just a basic way on how to change the font size
>> of the contents of a NSTextView, using code, not a font panel.
>
> With an NSTextView, you have to adjust the font for every style run
> individually. Luckily in Panther this is really easy: call
> changeAttributes
> and implement convertAttributes. Your convertAttributes will
> automatically
> be called for each style run, so you can set the font size and return
> the
> adjusted attributes dictionary. m.
There is also a superclass method setFont:, which will set the font for
the entire text. Be aware, though, that this does not include the
calls to shouldChangeTextInRange:replacementString:/didChangeText that
should be used for changes that are intended to respond to user
actions, so you would need to do those yourself, if for example you
wanted these changes to be undoable. Maybe something like this:
unsigned textLength = [[textView textStorage] length];
if (textLength > 0 && [textView
shouldChangeTextInRange:NSMakeRange(0, textLength)
replacementString:nil]) {
[textView setFont:newFont];
[textView didChangeText];
} else if (textLength == 0) {
[textView setFont:newFont]; // when text is empty, this just
changes the typing attributes
}
For a non-editable textview, such as e.g. FontBook usually uses,
setFont: by itself would be fine.
Douglas Davidson
| Related mails | Author | Date |
|---|---|---|
| Troy Payne | Oct 29, 21:11 | |
| Lon Varscsak | Oct 29, 22:17 | |
| Matt Neuburg | Nov 1, 17:47 | |
| Douglas Davidson | Nov 1, 18:39 |






Cocoa mail archive

