Displaying rtf string in editor gadget

Mac OSX specific forum
Totusoft
User
User
Posts: 15
Joined: Tue Feb 12, 2013 4:00 pm
Location: Minnesota, USA
Contact:

Displaying rtf string in editor gadget

Post by Totusoft »

I'm trying to display some rtf data that is in a string into a editor gadget. I've tried "Loading a rtf file" by Wilbert - (just to test getting some rtf data to display correctly). That works great displaying rtf from a file. I played around with that routine trying to use "initWithRTF:" instead. I'm guessing that's what I want to use, but am having a hard time figuring out how to use it correctly. Any help on how to use this would be much appreciated. I've searched on the forum, but am finding that most the examples are Windows based - not much for the Mac.

-Pete
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Displaying rtf string in editor gadget

Post by wilbert »

If you want a flexible solution that can set both rtf and html text, you can do it like this

Code: Select all

Procedure SetEditorText(EditorID, Text.s, Type.s = "NSPlainText")
  
  ; 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



If OpenWindow(0, 0, 0, 320, 150, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  EditorGadget(0, 10, 10, 300, 130)
  
  SetEditorText(GadgetID(0), "{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard"+ #LF$ + "This is some {\b bold} text.\par" + #LF$ + "}", "NSRTF")
  
  ;SetEditorText(GadgetID(0), "This is some <b>bold</b> text.", "NSHTML")
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  
EndIf
It does actually support even more formats ( "NSDocFormat", "NSWordML", "NSWebArchive", "NSOfficeOpenXML", "NSOpenDocument" ) but those don't fit very well inside a PureBasic string.
Windows (x64)
Raspberry Pi OS (Arm64)
Totusoft
User
User
Posts: 15
Joined: Tue Feb 12, 2013 4:00 pm
Location: Minnesota, USA
Contact:

Re: Displaying rtf string in editor gadget

Post by Totusoft »

Wilbert,

Thank you for this example! I am gone for the weekend, but can't wait to try it on Monday.

-Pete
Totusoft
User
User
Posts: 15
Joined: Tue Feb 12, 2013 4:00 pm
Location: Minnesota, USA
Contact:

Re: Displaying rtf string in editor gadget

Post by Totusoft »

Your routine worked great! Thanks Wilbert. I'm coming from Windows API, and trying to catch onto Cocoa Methods. I see I still have a long ways to go - because I was way off ;)

-Pete
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Displaying rtf string in editor gadget

Post by wilbert »

Glad to hear it worked :)
As for Cocoa, once you understand it a bit, it makes sense. :idea:
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply