Problems setting font for text area

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.

Thanks!

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.

Hello,

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.

Regards,

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_


Hello,

You have two problems here.

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:

NSTextStorage : NSMutableAttributedString : NSAttributedString : NSObject

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.

You can look at the doc here:
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/AttributedStrings/Concepts/AttrStrings.html#//apple_ref/doc/uid/20000160-BAJJGCAI

Regards,

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?

Hi,

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.

textView's textStorage()'s setAttributedString_(anAttributedString)

Pass the font with the key NSFontAttributeName in the attributes dictionary

Hi Stefan.

No, it does not.

If I set the text color of a NSTextView to red and the font to Monaco 14 in IB and then write


[self.textView setString:@"Hello"];

“Hello” is always Helvetica 10 and black (the default attributes of NSAttributedString)

I set the NSTextView to KENDRIC Bold 24.0, color red, and when I execute this:

The text “HELLO” appears in KENDRIC Bold 24.0, color red. When I click on my button, “WORLD” appears in KENDRIC Bold 24.0, color red.

The problem is somewhere else, in the IB settings. How can I send you both my project?