Can I underline the text of a NSTextField from interface builder?
Asked Answered
R

2

10

Can I underline the text of a NSTextField from interface builder ?

So far, I've only able to change its color. Is there a way to underline it ?

thanks

Remittent answered 10/2, 2012 at 16:4 Comment(1)
check this out: [link][1] [1]: #6487790Hutment
R
18

No, you'd have to do it in code. For example:

NSMutableAttributedString *str = [[myTextField attributedStringValue] mutableCopy];

[str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(0, str.length)];

[myTextField setAttributedStringValue:str];

[str release];
Robbie answered 10/2, 2012 at 18:26 Comment(2)
This is a big help, though I ran into an error until I changed the value to: NSUnderlineStyleSingleGuild
NSMutableAttributedString *strs = [[NSMutableAttributedString alloc]initWithAttributedString:[btnPreviousTitle attributedTitle]]; NSRange range = NSMakeRange(0, [strs length]); [strs addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSSingleUnderlineStyle] range:range]; [btnPreviousTitle setAttributedTitle:strs]; help me to make my NSButton with Underline text.Carmellacarmelle
S
29

Valid only for iOS

Yes, set attributed text, then select the text, right mouse click > Font > Underline

enter image description here

Scarface answered 25/2, 2015 at 14:6 Comment(7)
Are you sure that screenshot goes with NSTextField? I do not see such an option to set the title to be attributed and it seems like that screenshot relates to a button instead.Amuse
It should do that with every Attributed String fieldScarface
I don't see an option for setting the string to be attributed. Are you sure that you are in an OS X app? Inside of Xcode when selecting a NSTextField I see the following: i.imgur.com/yTip3sq.pngAmuse
Nope, sorry, I'm on iOS but I supposed that that having an attributedString property would have been reflected in Interface Builder like they do with the other controls...Scarface
Yeah, I figured you misread the question, might want to add a note that your answer is for UITextField and not NSTextFieldAmuse
although it is useful info, the question was about NSTextfield not UI somethingSpank
Why is this answer here, and why are people upvoting it? Is it so hard to read the question title before answering?Dickenson
R
18

No, you'd have to do it in code. For example:

NSMutableAttributedString *str = [[myTextField attributedStringValue] mutableCopy];

[str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(0, str.length)];

[myTextField setAttributedStringValue:str];

[str release];
Robbie answered 10/2, 2012 at 18:26 Comment(2)
This is a big help, though I ran into an error until I changed the value to: NSUnderlineStyleSingleGuild
NSMutableAttributedString *strs = [[NSMutableAttributedString alloc]initWithAttributedString:[btnPreviousTitle attributedTitle]]; NSRange range = NSMakeRange(0, [strs length]); [strs addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSSingleUnderlineStyle] range:range]; [btnPreviousTitle setAttributedTitle:strs]; help me to make my NSButton with Underline text.Carmellacarmelle

© 2022 - 2024 — McMap. All rights reserved.