It is currently Mon Dec 11, 2017 11:53 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Italic Underline Bold in EditorGadget [Resolved]
PostPosted: Mon Mar 20, 2017 4:14 am 
Offline
New User
New User

Joined: Mon Jan 25, 2016 9:49 am
Posts: 7
How to change the text italic or underline in Editorgadget?

I know to make text bold:

Code:
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.

Top
 Profile  
Reply with quote  
 Post subject: Re: Italic Underline Bold in EditorGadget
PostPosted: Mon Mar 20, 2017 6:49 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3009
Location: Netherlands
I haven't verified it but it looks like you need to use fontWithDescriptor to create the font.

Something like
Code:
#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)

_________________
MacOS 10.13.1 High Sierra, PB 5.60 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: Italic Underline Bold in EditorGadget
PostPosted: Mon Mar 20, 2017 4:51 pm 
Offline
New User
New User

Joined: Mon Jan 25, 2016 9:49 am
Posts: 7
Thank you
This method give me bold + italic
but not italic only

Code:
          #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


Top
 Profile  
Reply with quote  
 Post subject: Re: Italic Underline Bold in EditorGadget
PostPosted: Mon Mar 20, 2017 9:35 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu May 30, 2013 4:39 pm
Posts: 350
try
Code:
CocoaMessage(0, 0, "NSFont systemFontOfSize:@", @FontSize)
instead of
Code:
CocoaMessage(0, 0, "NSFont boldSystemFontOfSize:@", @FontSize)


Top
 Profile  
Reply with quote  
 Post subject: Re: Italic Underline Bold in EditorGadget
PostPosted: Fri Mar 24, 2017 2:45 am 
Offline
New User
New User

Joined: Mon Jan 25, 2016 9:49 am
Posts: 7
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?


Top
 Profile  
Reply with quote  
 Post subject: Re: Italic Underline Bold in EditorGadget
PostPosted: Fri Mar 24, 2017 11:43 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3009
Location: Netherlands
Code:
#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

_________________
MacOS 10.13.1 High Sierra, PB 5.60 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: Italic Underline Bold in EditorGadget
PostPosted: Tue Mar 28, 2017 3:50 pm 
Offline
New User
New User

Joined: Mon Jan 25, 2016 9:49 am
Posts: 7
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Italic Underline Bold in EditorGadget
PostPosted: Tue Mar 28, 2017 5:41 pm 
Offline
Addict
Addict

Joined: Mon Aug 04, 2008 10:56 pm
Posts: 1100
Location: Seattle, USA
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-r, OSX 10.11.6, Command Line Tools 7.3 (10.11), PB-5.60x64


Top
 Profile  
Reply with quote  
 Post subject: Italic Underline Bold in EditorGadget [Resolved]
PostPosted: Wed Mar 29, 2017 5:02 pm 
Offline
New User
New User

Joined: Mon Jan 25, 2016 9:49 am
Posts: 7
Thank you,

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

Regards


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye