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
Displaying rtf string in editor gadget
Re: Displaying rtf string in editor gadget
If you want a flexible solution that can set both rtf and html text, you can do it like this
It does actually support even more formats ( "NSDocFormat", "NSWordML", "NSWebArchive", "NSOfficeOpenXML", "NSOpenDocument" ) but those don't fit very well inside a PureBasic string.
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
EndIfWindows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
Re: Displaying rtf string in editor gadget
Wilbert,
Thank you for this example! I am gone for the weekend, but can't wait to try it on Monday.
-Pete
Thank you for this example! I am gone for the weekend, but can't wait to try it on Monday.
-Pete
Re: Displaying rtf string in editor gadget
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
-Pete
Re: Displaying rtf string in editor gadget
Glad to hear it worked
As for Cocoa, once you understand it a bit, it makes sense.
As for Cocoa, once you understand it a bit, it makes sense.
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)

