Page 1 of 1

Text Bold

Posted: Wed Aug 20, 2014 7:55 pm
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)

Re: Text Bold

Posted: Fri Aug 29, 2014 5:48 pm
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)

Re: Text Bold

Posted: Fri Aug 29, 2014 7:12 pm
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

Re: Text Bold

Posted: Fri Aug 29, 2014 9:18 pm
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

Re: Text Bold

Posted: Sat Aug 30, 2014 7:33 am
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.

Re: Text Bold

Posted: Sat Aug 30, 2014 9:59 pm
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.

Re: Text Bold

Posted: Sun Aug 31, 2014 5:59 am
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.

Re: Text Bold

Posted: Sun Aug 31, 2014 3:48 pm
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 :)

Re: Text Bold

Posted: Sun Aug 31, 2014 4:24 pm
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)

Re: Text Bold

Posted: Thu Sep 11, 2014 9:05 pm
by spacebuddy
This is interesting Wilbert, but how would you check if the text is already bold?

Re: Text Bold

Posted: Fri Sep 12, 2014 6:30 am
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.