Text Bold

Mac OSX specific forum
spacebuddy
Enthusiast
Enthusiast
Posts: 346
Joined: Thu Jul 02, 2009 5:42 am

Text Bold

Post by spacebuddy »

is this code possible to convert to PB??????

[self.textView.attributedString addAttribute:NSFontAttributeName value:[NSFont boldSystemFontOfSize:12] range:self.textView.selectedRange];


I try different days but crashing for me with errors :shock:

This is the code I am trying.

#NSItalicFontMask = 1
#NSBoldFontMask = 2
range.NSRange
CocoaMessage(@Range.NSRange, GadgetID(0), "selectedRange")
CocoaMessage(0, GadgetID(0), "NSFontAttributeName", #NSBoldFontMask | # NSItalicFontMask, "range:@", @Range)
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Text Bold

Post by wilbert »

Assuming the Gadget is an EditorGadget ...

Making text bold

Code: Select all

CocoaMessage(@Range.NSRange, GadgetID(0), "selectedRange")

TextStorage.i = CocoaMessage(0, GadgetID(0), "textStorage")

FontSize.CGFloat = 12.0
BoldSystemFont.i = CocoaMessage(0, 0, "NSFont boldSystemFontOfSize:@", @FontSize)

CocoaMessage(0, TextStorage, "addAttribute:$", @"NSFont", "value:", BoldSystemFont, "range:@", @Range)
Making text red

Code: Select all

CocoaMessage(@Range.NSRange, GadgetID(0), "selectedRange")

TextStorage.i = CocoaMessage(0, GadgetID(0), "textStorage")

RedColor.i = CocoaMessage(0, 0, "NSColor redColor")

CocoaMessage(0, TextStorage, "addAttribute:$", @"NSColor", "value:", RedColor, "range:@", @Range)
Making background color yellow

Code: Select all

CocoaMessage(@Range.NSRange, GadgetID(0), "selectedRange")

TextStorage.i = CocoaMessage(0, GadgetID(0), "textStorage")

YellowColor.i = CocoaMessage(0, 0, "NSColor yellowColor")

CocoaMessage(0, TextStorage, "addAttribute:$", @"NSBackgroundColor", "value:", YellowColor, "range:@", @Range)
Windows (x64)
Raspberry Pi OS (Arm64)
spacebuddy
Enthusiast
Enthusiast
Posts: 346
Joined: Thu Jul 02, 2009 5:42 am

Re: Text Bold

Post by spacebuddy »

Thank Wilbert, that worked :D

Do you know how to set the font back to normal after it is bold??

Okay, I tried this and it seems to work
DefaultSystemFont.i = CocoaMessage(0, 0, "NSFont controlContentFontOfSize:@", @FontSize)
CocoaMessage(0, TextStorage, "addAttribute:$", @"NSFont", "value:", DefaultSystemFont, "range:@", @Range)

:D
WilliamL
Addict
Addict
Posts: 1214
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Text Bold

Post by WilliamL »

Wow, that sounds interesting!

Hey, spacebuddy, do you have a small example of selecting, bolding and coloring? (reset would be interesting too)

lets see... I've got this from wilbert (I think), it appears that the colors can be simplified and bolding added.

This is what I've go so far - it is a short form to do background,text color, and bold text.

Code: Select all

Procedure SetTextColor(EditorGadget, TextColor$, StartPosition, Length, BackColor$)
  Protected.CGFloat r,g,b,a
  Protected range.NSRange, textStorage.i
  If StartPosition > 0
    textStorage = CocoaMessage(0, GadgetID(EditorGadget), "textStorage")
    range\location = StartPosition - 1
    range\length = CocoaMessage(0, textStorage, "length") - range\location
    If range\length > 0
      If Length >= 0 And Length < range\length
        range\length = Length
      EndIf
    
      ;background color
      BckColor.i = CocoaMessage(0, 0, BackColor$)
      CocoaMessage(0, TextStorage, "addAttribute:$", @"NSBackgroundColor", "value:", BckColor, "range:@", @Range)
      
      ;set selection text color
      ForeColor.i=CocoaMessage(0,0,TextColor$)
      CocoaMessage(0,TextStorage,"addAttribute:$",@"NSColor","value:",ForeColor,"range:@",@Range)
      
      ;bold
      FontSize.CGFloat = 12.0
      BoldSystemFont.i = CocoaMessage(0, 0, "NSFont boldSystemFontOfSize:@", @FontSize)
      CocoaMessage(0, TextStorage, "addAttribute:$", @"NSFont", "value:", BoldSystemFont, "range:@", @Range)
    EndIf
  EndIf
EndProcedure

Procedure ResetAttribute(EditorGadget,StartPosition, Length)
  Protected.CGFloat r,g,b,a
  Protected range.NSRange, textStorage.i
  If StartPosition > 0
    textStorage = CocoaMessage(0, GadgetID(EditorGadget), "textStorage")
    range\location = StartPosition - 1
    range\length = CocoaMessage(0, textStorage, "length") - range\location
    If range\length > 0
      If Length >= 0 And Length < range\length
        range\length = Length
      EndIf
      CocoaMessage(0, TextStorage, "removeAttribute:$", @"NSFont", "range:@", @Range) ; remove bold
      CocoaMessage(0, TextStorage, "removeAttribute:$", @"NSColor", "range:@", @range) ; remove color
      CocoaMessage(0, TextStorage, "removeAttribute:$", @"NSBackgroundColor", "range:@", @range) ; remove background color
    EndIf
  EndIf
EndProcedure

If OpenWindow(0, 0, 0, 322, 150, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  EditorGadget(0, 8, 28, 306, 113)
  SetGadgetText(0, "This is a test string to test if coloring" + #CRLF$ + "specific areas will work")
  
  SetTextColor(0,"NSColor redColor", 1,7,"NSColor yellowColor"); make selection red with yellow background with bold text
  
  ButtonGadget(1,10,2,80,20,"Deselect")
  
    Repeat
        Event = WaitWindowEvent()
        Select Event
        Case #PB_Event_Gadget
            Select EventGadget()
            Case 1 ; : Debug "Button 1 clicked!"
                resetAttribute(0,1,7)
            EndSelect
        EndSelect
    Until Event = #PB_Event_CloseWindow    
EndIf
Last edited by WilliamL on Sun Aug 31, 2014 5:01 pm, edited 4 times in total.
MacBook Pro-M1 (2021), Sonoma 14.3.1 (CLT 15.3), PB 6.10b7 M1
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Text Bold

Post by wilbert »

The TextStorage object itself doesn't change.
You can get it once after the EditorGadget has been created and store it into a global variable or something like that.
For more information, you can look at the "NSMutableAttributedString Class Reference" .
It has a method removeAttribute:(NSString *)name range:(NSRange)aRange that can be used to remove an attribute.
Windows (x64)
Raspberry Pi OS (Arm64)
WilliamL
Addict
Addict
Posts: 1214
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Text Bold

Post by WilliamL »

Hey, wilbert, don't make me wade thru the 'NSMutableAttributed String Class Reference'!

I get the idea about the TextStorage variable (it doesn't need to be re-loaded all the time). My example works above but what would be interesting is to have a 'all' re-set of the string attributes to default (no bold/text color/text background). Is there a 'removeAllAttribute'? (for the selected range)

...and how does this go? (this is just garbage ;-)
CocoaMessage(0,textstorage,"removeAttribute:$", @"NSBackgroundColor"," range:(NSRange)",@Range)

Well, i suppose I could just write the text to the EditorGadget again and it would clear everything out.
MacBook Pro-M1 (2021), Sonoma 14.3.1 (CLT 15.3), PB 6.10b7 M1
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Text Bold

Post by wilbert »

WilliamL wrote:...and how does this go? (this is just garbage
CocoaMessage(0,textstorage,"removeAttribute:$", @"NSBackgroundColor"," range:(NSRange)",@Range)

Code: Select all

CocoaMessage(0, TextStorage, "removeAttribute:$", @"NSBackgroundColor", "range:@", @Range)
WilliamL wrote:Well, i suppose I could just write the text to the EditorGadget again and it would clear everything out.
I think that would be the easiest solution since there is no method to remove all attributes as far as I can tell.
Windows (x64)
Raspberry Pi OS (Arm64)
WilliamL
Addict
Addict
Posts: 1214
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Text Bold

Post by WilliamL »

Code: Select all

CocoaMessage(0, TextStorage, "removeAttribute:$", @"NSBackgroundColor", "range:@", @range)
Boy, you'd think I'd guess that :shock:

It works fine except for removing the bold attribute (see example above). I don't think I'd use bold so I guess I'm not too concerned about it.

Thanks, wilbert :)
MacBook Pro-M1 (2021), Sonoma 14.3.1 (CLT 15.3), PB 6.10b7 M1
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Text Bold

Post by wilbert »

WilliamL wrote:It works fine except for removing the bold attribute (see example above). I don't think I'd use bold so I guess I'm not too concerned about it.
There are several ways to do that.

1. Use font traits to make text bold or unbold.

Code: Select all

#NSBoldFontMask = 2
#NSUnboldFontMask = 4

CocoaMessage(0, TextStorage, "applyFontTraits:", #NSBoldFontMask, "range:@", @Range)

CocoaMessage(0, TextStorage, "applyFontTraits:", #NSUnboldFontMask, "range:@", @Range)
2. Remove all font information

Code: Select all

CocoaMessage(0, TextStorage, "removeAttribute:$", @"NSFont", "range:@", @Range)
3. Overwrite the font attribute with a font that isn't bold.

Code: Select all

FontSize.CGFloat = 12.0
SystemFont.i = CocoaMessage(0, 0, "NSFont systemFontOfSize:@", @FontSize)
CocoaMessage(0, TextStorage, "addAttribute:$", @"NSFont", "value:", SystemFont, "range:@", @Range)
Windows (x64)
Raspberry Pi OS (Arm64)
spacebuddy
Enthusiast
Enthusiast
Posts: 346
Joined: Thu Jul 02, 2009 5:42 am

Re: Text Bold

Post by spacebuddy »

This is interesting Wilbert, but how would you check if the text is already bold?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Text Bold

Post by wilbert »

spacebuddy wrote:how would you check if the text is already bold?
Interesting question Spacebuddy.

Assuming you already got the TextStorage object from the EditorGadget with
TextStorage = CocoaMessage(0, GadgetID(EditorGadget), "textStorage")
you can check like this

Code: Select all

#NSFontBoldTrait = 1 << 1

NSFont.i = CocoaMessage(0, TextStorage, "attribute:$", @"NSFont", "atIndex:", 3, "effectiveRange:", @Range.NSRange)
NSFontSymbolicTraits.l = CocoaMessage(0, CocoaMessage(0, NSFont, "fontDescriptor"), "symbolicTraits")

If NSFontSymbolicTraits & #NSFontBoldTrait
  Debug "TextIsBold"
EndIf
A few remarks on the line that gets the attribute ...
atIndex:
specifies the zero based position you wish to examine.

effectiveRange:
a pointer to a NSRange object.
Since it's a pointer that is expected in this case, you should NOT put a @ after effectiveRange !
What it returns is the range that covers the attribute. So if the same font is used on position 0 to 10 and you check position 3, the range from 0 to 10 is returned.
If you don't need this information, you can simple pass #Null instead.
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply