I’ve built an app with a non-editable scrolling text area. I’d like to apply a font to the text there, but I can’t seem to get it to work.
In XCode, I’ve set the properties so that there’s a styled default value. It shows up fine when the app launches. But once I populate the text area with new text, it reverts to Helvetica – the default system font.
How do I make it retain the default font settings? I’m just passing a string, not styled text.
Without looking at anything, I know that it involves text storage and attributed strings. Are you using attributed strings?
You would be better off not setting the text style using the “interface builder” tools in XCode but then setting the styles you want on awakeFromNib. Then you can have a pointer to the string’s attributes.
Sorry it’s not code sample but maybe this will put you closer on the right path.
In the NSTextView Attribute Inspector, you have two text fields. The first one is to choose the font. Try pasting an styled text on the second field. Your textView should now use this attributed string as default. I just tried.
I don’t think I’m using attributed strings, on account of I don’t know what they are.
My code is something like this:
property mainWindow: missing value -- bound to the containing window
property myTextView: missing value -- bound to the NSTextView
...
on updateMyTextView_(s)
set my myTextView to (s as text)
tell mainWindow to displayIfNeeded()
end updateMyTextView_
First, your myTextView is a NSTextView. It does not have a “text” property, but a “textStorage” one. This textStorage is a NSTextStorage object, whose inheritance is:
Second, if you change a property with “my”, the KVO mechanism will update your interface, so you don’t have to call displayIfNeeded() – above all NOT for the whole window.
Try something like this:
Regarding the text style, I assure you that you can set the font, size, style and even color entirely in IB. This works with a NSTextField as well as a NSTextView (once again, I just tried).
Be aware to select the text view, and not the enclosing ScrollView! You should see the text settings on the Attributes Inspector (the “slider” icon on the toolbar). You can give any attribute to your text, and they will apply when you put some text into the textView.
Note: An “attributed string” is a string with formatting attributes, allowing to format each character of the string differently, if you want, like in a text processing application. NSString can only be attributed a single style. Note that NSAttributedString does NOT inherit from NSString, but from NSObject.
Bernard, I can definitely set text defaults in IB, and they show up in the default value and if I edit the view directly. But once I assign a new string to the text view, it reverts to the system font.
Do I need to convert the string into an NSString before applying it or something?
the method setString: of NSTextView ignores predefined attributes like font or color.
You need to apply an NSAttributedString to the textStorage object of the text view e.g.