Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextField (Label) Attributed Text: Select

I have a NSTextField Label that has attributed text in it. The color and size are different from the defaults. (The text is a bit larger and green) Now, I need to be able to select the text in the label so as the click the hyperlinks embedded in the attributed text. This works fine; however, when selecting the text the format of the text reverts back to the defaults (a smaller, black font).

Is there anyway to stop this behavior so that my styling is preserved when the user clicks (selects text) on a Label?

EDIT: For the record, I have another label with white text that does not change color when selected, only size.

like image 703
jhleath Avatar asked May 30 '10 19:05

jhleath


2 Answers

I ran into the same issue. The accepted answer didn't fix it unfortunately. My text style was still changing when being selected even though I had set the attributedStringValue.

Thanks to the response in this question, I was able to in part solve the problem by calling:

label.allowsEditingTextAttributes = true

like image 94
Wesley McCloy Avatar answered Sep 23 '22 09:09

Wesley McCloy


Okay. After further research as to why the white color was 'sticking' and why the green was not, I discovered that any text attributes set in Interface Builder do not stay after selection.

Therefore, the solution is to always call setAttributedStringValue: with an NSAttributedString that has attributes that you want to make sure stay during selection on your label.

like image 41
jhleath Avatar answered Sep 21 '22 09:09

jhleath