Text Bold
-
- Enthusiast
- Posts: 346
- Joined: Thu Jul 02, 2009 5:42 am
Text Bold
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
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)
[self.textView.attributedString addAttribute:NSFontAttributeName value:[NSFont boldSystemFontOfSize:12] range:self.textView.selectedRange];
I try different days but crashing for me with errors
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
Assuming the Gadget is an EditorGadget ...
Making text bold
Making text red
Making background color yellow
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)
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)
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)
Raspberry Pi OS (Arm64)
-
- Enthusiast
- Posts: 346
- Joined: Thu Jul 02, 2009 5:42 am
Re: Text Bold
Thank Wilbert, that worked
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)
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)
Re: Text Bold
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.
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
Re: Text Bold
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.
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)
Raspberry Pi OS (Arm64)
Re: Text Bold
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.
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
Re: Text Bold
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)
I think that would be the easiest solution since there is no method to remove all attributes as far as I can tell.WilliamL wrote:Well, i suppose I could just write the text to the EditorGadget again and it would clear everything out.
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
Re: Text Bold
Code: Select all
CocoaMessage(0, TextStorage, "removeAttribute:$", @"NSBackgroundColor", "range:@", @range)
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
Re: Text Bold
There are several ways to do that.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.
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)
Code: Select all
CocoaMessage(0, TextStorage, "removeAttribute:$", @"NSFont", "range:@", @Range)
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)
Raspberry Pi OS (Arm64)
-
- Enthusiast
- Posts: 346
- Joined: Thu Jul 02, 2009 5:42 am
Re: Text Bold
This is interesting Wilbert, but how would you check if the text is already bold?
Re: Text Bold
Interesting question Spacebuddy.spacebuddy wrote:how would you check if the text is already bold?
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
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)
Raspberry Pi OS (Arm64)