I banged this out yesterday for my day-job, and thought it might make a good example of how I use all of this. I added it as a method to the CojoTextArea subclass.
My process was to find a reasonable solution to the problem at hand (adding custom tab stops to a TextArea.) I found code posted on StackOverflow. I was able to read through the code line by line, translating to my dot syntax and adding comments as I went;
[code]Sub SetTextWithTabs(aString as String, tabs() as Single)
’ This mess overrides the default tab stops for NSTextView.
’ See the following link for details;
'http://stackoverflow.com/questions/14278119/premature-line-wrapping-in-nstextview-when-tabs-are-used
’ Pass in the text you want in the field, and an array of Singles describing where you want the tab stops, based on
’ 72 points per inch. This could probably be readliy adapted to other measurement systems.
’ ----------
dim points_per as Single = 72.0
dim tabArray as new CojoArray
dim cn as Integer = tabs.Ubound
’ Loop through the incoming array of singles, and make NSTextTab objects out of them.
’ Pack them into an NSMutableArray very easily using a CojoArray.
for idx as integer = 0 to cn
dim aTab as new NSPtr( "NSTextTab" ) ' New instance.
Call aTab.Alloc ' Allocated.
' Now init with our value.
aTab = Cojo.NSTextTab.initWithType( aTab, Cojo.NSTextTabType.NSLeftTabStopType, ( tabs( idx ) * points_per ) )
' Add the object to the CojoArray.
tabArray.Append( aTab )
next
’ Create a new paragraph style with our array of tabs.
dim aMutableParagraphStyle as NSPtr = Cojo.NSParagraphStyle.defaultParagraphStyle.MutableCopy
Cojo.NSMutableParagraphStyle.setTabStops( aMutableParagraphStyle, tabArray.NSPtr )
’ Here we initialize an attributed string with our incoming string, and make a mutable copy.
dim attributedString as new NSPtr( “NSAttributedString” )
Call attributedString.Alloc
attributedString = Cojo.NSAttributedString.initWithString( attributedString, aString ).MutableCopy
’ We need a range describing all the text.
dim aRange as NSRange
aRange.location = 0
aRange.length = Len( aString )
’ Now we apply the paragraph style over that range.
dim nam as String = Cojo.StringConstant( “NSParagraphStyleAttributeName” )
Cojo.NSMutableAttributedString.addAttribute( attributedString, nam, aMutableParagraphStyle, aRange )
’ Now we drill down from TextArea to its Document view, and then that thing’s Text Storage.
dim docView as NSPtr = Cojo.NSScrollView.DocumentView( me.NSPtr )
dim aTextStorage as NSPtr = Cojo.NSTextView.textStorage( docView )
’ Finally, we plug our attributed string into the text storage, and voila!
Cojo.NSMutableAttributedString.setAttributedString( aTextStorage, attributedString )
’ These force the TextArea to redraw with our spiffy new contents.
me.SetFocus
me.Refresh
End Sub
[/code]
I also set the following values in the open event of the TextArea so I can see the ruler;
me.isRichText = True
me.usesFindPanel = True
me.isRulerVisible = True
This process of translation works phenomenally well in my brain. Not sure everyone will agree.