« Free Tier for MBS Fil… | Home | MBS FileMaker Plugin,… »

RTF functions in MBS Plugins

We have a lot of RTF related functions in MBS Xojo Plugins:

Our RTF extension to the Xojo StyledText class is great to speed up loading and saving of StyledText objects. If the styled text belongs to a (Desktop)TextArea, you may use our RTFDataMBS or WinRTFDataMBS functions directly to load and save RTF data. The functions use the OS functions, so they support more styles and font options.

For example if you use WinShowFontPanelMBS to show the Windows font panel or NSFontPanelMBS class for macOS, you may get more font styles than what Xojo supports directly, e.g. double underlined.

Let us show you an example on how to load RTF with our functions:

Sub LoadRTF() Dim f As FolderItem = GetOpenFolderItem(FileTypes1.Rtf) If f<>Nil Then Dim b As BinaryStream = BinaryStream.Open(f) Dim s As String = b.Read(b.Length) #If TargetWindows Then TextArea1.WinRTFDataMBS(False) = s #Elseif TargetMacOS then Dim n As NSTextViewMBS = TextArea1.NSTextViewMBS Dim a As NSAttributedStringMBS = NSAttributedStringMBS.attributedStringWithRTF(s) n.textStorage.setAttributedString a #EndIf End If End Sub

And other way around to load the RTF:

Sub SaveRTF() Dim f As FolderItem = GetSaveFolderItem(FileTypes1.Rtf, "test.rtf") If f<>Nil Then #If TargetWindows then Dim s As String = TextArea1.WinRTFDataMBS(False) #ElseIf TargetMacOS Then Dim n As NSTextViewMBS = TextArea1.NSTextViewMBS Dim a As NSAttributedStringMBS = n.textStorage Dim s As String = a.rtf #EndIf Dim b As BinaryStream = BinaryStream.Create(f, True) b.Write s End If End Sub

If you like to insert an image, you can even do that:

Sub InsertImage() Dim f As FolderItem = GetOpenFolderItem(FileTypes1.ImageXBmp+FileTypes1.Png+FileTypes1.Jpeg) If f<>Nil Then #If TargetWindows Then dim p as Picture = Picture.Open(f) If p<>Nil then dim b as BinaryStream = BinaryStream.Open(f) dim d as string = b.Read(b.Length) TextArea1.WinInsertImageMBS(d, p.Width, p.Height) End If #ElseIf TargetMacOS Then Dim image As New NSImageMBS(f) // build attachment Dim imageAttachment As New NSTextAttachmentMBS(image) Dim attributedString As NSAttributedStringMBS = NSAttributedStringMBS.attributedStringWithAttachment(imageAttachment) // insert image Dim textView As NSTextViewMBS = TextArea1.NSTextViewMBS textview.insertText attributedString #EndIf End If End Sub

Please note that Windows saves the images inline in the RTF file. But macOS uses RTFD files, which need separate handling for loading and saving. But we have plenty of RTFD functions as you see above.

Let us know if you have questions. Or check the RTF Windows example with next plugin version to copy the sample code.

30 01 23 - 10:58