Italic Underline Bold in EditorGadget [Resolved]

Mac OSX specific forum
taoteblues
New User
New User
Posts: 7
Joined: Mon Jan 25, 2016 9:49 am

Italic Underline Bold in EditorGadget [Resolved]

Post by taoteblues »

How to change the text italic or underline in Editorgadget?

I know to make 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)
Last edited by taoteblues on Wed Mar 29, 2017 5:10 pm, edited 1 time in total.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Italic Underline Bold in EditorGadget

Post by wilbert »

I haven't verified it but it looks like you need to use fontWithDescriptor to create the font.

Something like

Code: Select all

#NSFontItalicTrait = 1

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

FontDescriptor = CocoaMessage(0, BoldSystemFont, "fontDescriptor")
SymbolicTraits = CocoaMessage(0, FontDescriptor, "symbolicTraits") | #NSFontItalicTrait
FontDescriptor = CocoaMessage(0, FontDescriptor, "fontDescriptorWithSymbolicTraits:", SymbolicTraits)
BoldItalicFont = CocoaMessage(0, 0, "NSFont fontWithDescriptor:", FontDescriptor, "size:@", @FontSize)
Windows (x64)
Raspberry Pi OS (Arm64)
taoteblues
New User
New User
Posts: 7
Joined: Mon Jan 25, 2016 9:49 am

Re: Italic Underline Bold in EditorGadget

Post by taoteblues »

Thank you
This method give me bold + italic
but not italic only

Code: Select all

          #NSFontItalicTrait = 1
 CocoaMessage(@Range.NSRange, GadgetID(#Editor_0), "selectedRange")
TextStorage.i = CocoaMessage(0, GadgetID(#Editor_0), "textStorage")
;FontSize.CGFloat = 12.0
BoldSystemFont.i = CocoaMessage(0, 0, "NSFont boldSystemFontOfSize:@", @FontSize)
FontDescriptor = CocoaMessage(0, BoldSystemFont, "fontDescriptor")
SymbolicTraits = CocoaMessage(0, FontDescriptor, "symbolicTraits") | #NSFontItalicTrait
FontDescriptor = CocoaMessage(0, FontDescriptor, "fontDescriptorWithSymbolicTraits:", SymbolicTraits)
BoldItalicFont.i = CocoaMessage(0, 0, "NSFont fontWithDescriptor:", FontDescriptor, "size:@", @FontSize)
CocoaMessage(0, TextStorage, "addAttribute:$", @"NSFont", "value:", BoldItalicFont, "range:@", @Range)
Regards
Wolfram
Enthusiast
Enthusiast
Posts: 567
Joined: Thu May 30, 2013 4:39 pm

Re: Italic Underline Bold in EditorGadget

Post by Wolfram »

try

Code: Select all

CocoaMessage(0, 0, "NSFont systemFontOfSize:@", @FontSize)
instead of

Code: Select all

CocoaMessage(0, 0, "NSFont boldSystemFontOfSize:@", @FontSize)
macOS Catalina 10.15.7
taoteblues
New User
New User
Posts: 7
Joined: Mon Jan 25, 2016 9:49 am

Re: Italic Underline Bold in EditorGadget

Post by taoteblues »

Thank you but it's always the same.
I change the value of ' #NSFontItalicTrait': 1, 2 or 3
Not possible to have only italic or only underline.
Have you another idea?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Italic Underline Bold in EditorGadget

Post by wilbert »

Code: Select all

#NSItalicFontMask = 1
#NSBoldFontMask   = 2

#NSUnderlineStyleNone = 0
#NSUnderlineStyleSingle = 1
#NSUnderlineStyleThick = 2
#NSUnderlineStyleDouble = 9
#NSUnderlinePatternSolid = 0
#NSUnderlinePatternDot = $100
#NSUnderlinePatternDash = $200
#NSUnderlinePatternDashDot = $300
#NSUnderlinePatternDashDotDot = $400
#NSUnderlineByWord = $8000


FontManager = CocoaMessage(0, 0, "NSFontManager sharedFontManager")
FontSize.CGFloat = 12

If OpenWindow(0, 0, 0, 322, 150, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  EditorGadget(0, 8, 8, 306, 133)
  SetGadgetText(0, "This is a test")
  
  TextStorage = CocoaMessage(0, GadgetID(0), "textStorage")
  
  
  Range.NSRange\location = 0
  Range\length = 4
  BoldFont = CocoaMessage(0, FontManager, "fontWithFamily:$", @"Helvetica",
                          "traits:", #NSBoldFontMask, 
                          "weight:", 0, "size:@", @FontSize)
  CocoaMessage(0, TextStorage, "addAttribute:$", @"NSFont", "value:", BoldFont, "range:@", @Range)
  
  
  ItalicFont = CocoaMessage(0, FontManager, "fontWithFamily:$", @"Helvetica",
                            "traits:", #NSItalicFontMask, 
                            "weight:", 0, "size:@", @FontSize)
  Range\location = 10
  Range\length = 4
  CocoaMessage(0, TextStorage, "addAttribute:$", @"NSFont", "value:", ItalicFont, "range:@", @Range)
  
  
  UnderlineStyle = CocoaMessage(0, 0, "NSNumber numberWithInt:",
                                #NSUnderlineStyleSingle | #NSUnderlineByWord)
  Range\location = 0
  Range\length = 9
  CocoaMessage(0, TextStorage, "addAttribute:$", @"NSUnderline", "value:", UnderlineStyle, "range:@", @Range)
  
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Windows (x64)
Raspberry Pi OS (Arm64)
taoteblues
New User
New User
Posts: 7
Joined: Mon Jan 25, 2016 9:49 am

Re: Italic Underline Bold in EditorGadget

Post by taoteblues »

Thank you,

It's ok now for italic and underline but not for bold. May be it's because I am with MACOS 10.5.8 and Purebasic 5.31 x32 ?

Code: Select all

Global FontSize.CGFloat = 12.0
Global TextStorage.i
Global BoldFont.i
Global ItalicFont.i
Global UnderlineFont.i
Global BoldItalicFont.i
#NSItalicFontMask = 1
#NSBoldFontMask = 2
#NSBoldItalicFontMask = 3

#NSUnderlineStyleNone = 0
#NSUnderlineStyleSingle = 1
#NSUnderlineStyleThick = 2
;#NSUnderlineStyleDouble = 9
;#NSUnderlinePatternSolid = 0
;#NSUnderlinePatternDot = $100
;#NSUnderlinePatternDash = $200
;#NSUnderlinePatternDashDot = $300
;#NSUnderlinePatternDashDotDot = $400
#NSUnderlineByWord = $8000

Enumeration FormWindow
  #Window_0
EndEnumeration


Enumeration FormGadget
  #Editor_0
  #Button_gras
  #Button_souligne
  #Button_italic
  #Button_grasitalic
EndEnumeration



FontManager = CocoaMessage(0, 0, "NSFontManager sharedFontManager")

Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
  OpenWindow(#Window_0, x, y, width, height, "", #PB_Window_SystemMenu)
  EditorGadget(#Editor_0, 20, 50, 560, 290)
  ButtonGadget(#Button_gras, 20, 10, 50, 30, "Bold")
  ButtonGadget(#Button_souligne, 80, 10, 60, 30, "Underline")
  ButtonGadget(#Button_italic, 150, 10, 70, 30, "Italic")
  ButtonGadget(#Button_grasitalic, 230, 10, 100, 30, "Bold Italic")
EndProcedure


Procedure SetEditorText(EditorID, Text.s, Type.s = "NSRTF")
  
  ; Type can be : "NSPlainText", "NSRTF", "NSHTML"
  
  Protected DataObj = CocoaMessage(0, CocoaMessage(0, 0, "NSString stringWithString:$", @Text), "dataUsingEncoding:", 4)
  Protected TypeDict = CocoaMessage(0, 0, "NSDictionary dictionaryWithObject:$", @Type, "forKey:$", @"DocumentType")
  
  Protected AttributedString = CocoaMessage(0, CocoaMessage(0, 0, "NSAttributedString alloc"), "initWithData:", DataObj, "options:", TypeDict, "documentAttributes:", #Null, "error:", #nil)
  If AttributedString
    CocoaMessage(0, CocoaMessage(0, EditorID, "textStorage"), "setAttributedString:", AttributedString)
    CocoaMessage(0, AttributedString, "release")
  EndIf
  
EndProcedure


OpenWindow_0()

 SetGadgetText(#Editor_0, "je fais un test en gras, souligne, italique.")
Repeat
  Select  WaitWindowEvent()
      
      
   Case #PB_Event_Gadget
   
     Select EventGadget()
        
        Case #Button_gras
          CocoaMessage(@Range.NSRange, GadgetID(#Editor_0), "selectedRange")
TextStorage = CocoaMessage(0, GadgetID(#Editor_0), "textStorage")

;BoldFont= CocoaMessage(0, 0, "NSFont boldSystemFontOfSize:@", @FontSize)
 
BoldFont = CocoaMessage(0, FontManager, "fontWithFamily:$", @"Helvetica","traits:", #NSBoldFontMask,"weight:", 0, "size:@", @FontSize)
  CocoaMessage(0, TextStorage, "addAttribute:$", @"NSFont", "value:", BoldFont, "range:@", @Range)
  
  
Case #Button_italic
  CocoaMessage(@Range.NSRange, GadgetID(#Editor_0), "selectedRange")
 TextStorage = CocoaMessage(0, GadgetID(#Editor_0), "textStorage")
 ItalicFont = CocoaMessage(0, FontManager, "fontWithFamily:$", @"Helvetica","traits:", #NSItalicFontMask,"weight:", 0, "size:@", @FontSize)

  CocoaMessage(0, TextStorage, "addAttribute:$", @"NSFont", "value:", ItalicFont, "range:@", @Range)

Case #Button_grasitalic
         CocoaMessage(@Range.NSRange, GadgetID(#Editor_0), "selectedRange")
TextStorage = CocoaMessage(0, GadgetID(#Editor_0), "textStorage")
BoldItalicFont = CocoaMessage(0, FontManager, "fontWithFamily:$", @"Helvetica","traits:", #NSBoldItalicFontMask,"weight:", 0, "size:@", @FontSize)

CocoaMessage(0, TextStorage, "addAttribute:$", @"NSFont", "value:", BoldItalicFont, "range:@", @Range)
 
          
        Case #Button_souligne
CocoaMessage(@Range.NSRange, GadgetID(#Editor_0), "selectedRange")
TextStorage = CocoaMessage(0, GadgetID(#Editor_0), "textStorage")
Underlinefont = CocoaMessage(0, 0, "NSNumber numberWithInt:", #NSUnderlineStyleSingle | #NSUnderlineByWord)
 
CocoaMessage(0, TextStorage, "addAttribute:$", @"NSUnderline", "value:", UnderlineFont, "range:@", @Range)
       
       EndSelect   
Case #PB_Event_CloseWindow 
End 
EndSelect
ForEver
WilliamL
Addict
Addict
Posts: 1214
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Italic Underline Bold in EditorGadget

Post by WilliamL »

This is what I get with wilbert's code:

Image

It's a little hard to see from the copy of my retina screen, but only 'This' is bold.
MacBook Pro-M1 (2021), Sonoma 14.3.1 (CLT 15.3), PB 6.10b7 M1
taoteblues
New User
New User
Posts: 7
Joined: Mon Jan 25, 2016 9:49 am

Italic Underline Bold in EditorGadget [Resolved]

Post by taoteblues »

Thank you,

May be, it's a problem with my poor MACOX 10.5.8 and Purebasic 5.31

Regards
Post Reply