The StyledText class introduced in the previous chapter is a relatively recent addition to REALbasic, and it provides an excellent way to manipulate the style of text in an EditField in very powerful ways. Two classes are involved: the StyledText class itself and the StyleRun class. A StyleRun represents a string of text whose characters all share the same style data. The properties of StyleRun follow; you can see the different style elements you can control over a region of text: StyleRun.Bold as Boolean StyleRun.Italic as Boolean StyleRun.Underline as Boolean StyleRun.Font as String StyleRun.Size as Integer StyleRun.TextColor as Color StyleRun.Text as String You gain access to StyleRun objects through the StyledText.StyleRun method. The StyleRun method is indexed, so pass a number to it to identify that particular StyleRun. This is often used in combination with the StyleRunCount method, as follows: Dim count as Integer Dim x as Integer count = thisStyledText.StyleRunCount For x = 0 to count - 1 thisStyledText.StyleRun(x).Bold = True Next In this example, all the characters in each style run a set to be bold. One thing you may notice missing from this list are styles relating to text alignment. That's because alignment is a style that is applied at the paragraph level rather than the character level, which makes sense because a paragraph can only have one alignment at a time. The StyledText class maintains a list of StyleRuns and paragraphs. There is a ParagraphCount property that will let you know how many paragraphs this text has. With that information, you can cycle through each paragraph according to the position, like so: Dim count as Integer Dim x as Integer count = thisStyledText.ParagraphCount For x = 0 to count - 1 thisStyledText.Paragraph(x).Alignment = StyledText.AlignCenter Next In this example, the alignment of every paragraph was set to be centered. The StyledText.Paragraph method returns a Paragraph object, and the Alignment property of that object was set using a StyledText class constant. Paragraphs have the following properties: Paragraph.Alignment as Integer Paragraph.StartPost as Integer Paragraph.EndPos as Integer Paragraph.Length as Integer The alignment constants are the following: StyledText.AlignDefault = 0 StyledText.AlignLeft = 1 StyledText.AlignCenter = 2 StyledText.AlignRight= 3 If you are working with an EditField, the only way to manipulate the styles of text within that EditField is with the methods of the StyledText class, not directly through access to StyleRuns. You can work directly with StyleRuns, but you have to remove the old StyleRun from the StyledText in the EditField and insert the new run. What you can't do is modify an existing StyleRun by directly accessing the properties of the StyleRun. This also means that if you are populating an EditField, you can do so with StyleRuns. You can add them to the StyledText object associated with the EditField. Apply Styles The following classes illustrate the use of the StyledText class in a font panel shown in Figure 6.2a small Window that you can use to get and set the text characteristics of text selected in an EditField in the main window. Figure 6.2. Font Panel. Window1 Inherits Window Window1 is the primary window for the application. It contains the EditField, whose style the Font Panel will manage. Listing 6.103. Sub Window1.Open() Handles Event FontPanel.show FontPanel.showFonts FontPanel.showSizes FontPanel.setEditField Window1.EditField1 | Listing 6.104. Sub SelChange() Handles Event // Update FontPanel so that controls display the current // style, based upon SelStart FontPanel.showCurrentStyle | Listing 6.105. FontPanel Inherits Window // A reference to the EditField // whose styles are being managed Property Field As EditField | Listing 6.106. Sub FontPanel.showFonts() // Display the list of available fonts // in the FontList ListBox. Dim y as Integer Dim x as Integer y = FontCount -1 For x = 0 To y Me.FontList.addRow(Font(x)) Next | Listing 6.107. Sub FontPanel.showSizes() // Populate the SizeList ListBox with // some commonly used font sizes sizeList.addRow("10") sizeList.addRow("11") sizeList.addRow("12") sizeList.addRow("14") sizeList.addRow("16") sizeList.addRow("18") sizeList.addRow("22") sizeList.addRow("26") sizeList.addRow("30") sizeList.addRow("36") | Listing 6.108. Sub FontPanel.setEditField(anEditField as EditField) // Make a reference to the EditField this // FontPanel will manage fonts for. me.Field = anEditField | Listing 6.109. Sub FontPanel.selectString(aListBox as ListBox, aString as String) // A generic method that allows you to find the row // in the ListBox whose text matches aString argument. Dim x,y as Integer y = aListBox.ListCount - 1 For x = 0 To y If aListBox.Cell(x,0) = aString Then aListBox.ListIndex = x Exit End If Next | Listing 6.110. Sub FontPanel.showCurrentStyle() // Get the values from the currently // selected string and set the state // of the controls on the FontPanel. // Select the current font in the FontList ListBox selectString FontList, Field.SelTextFont // Select the current font size in the SizeList Listbox selectString SizeList, Str(Field.SelTextSize) // Place the current font size in the FontSize EditField FontSize.Text = str(Field.SelTextSize) // Position the slider for the current font size SizeSlider.value = 144 - val(FontSize.text) // Toggle ButtonBold BevelButton If Field.SelBold Then ButtonBold.Value = True Else ButtonBold.Value = False End If // Toggle ButtonItalic BevelButon If Field.SelItalic Then ButtonItalic.Value = True Else ButtonItalic.Value = False End If // Toggle ButtonUnderline BevelButton If Field.SelUnderline Then ButtonUnderline.Value = True Else ButtonUnderline.Value = False End If // Select RadioButton according to alignment. // The RadioButtons are part of a Control Array Self.Alignment(Field.SelAlignment - 1).Value = True | Listing 6.111. Sub FontPanel.setCurrentStyle() // Set the value of the selected text according // to the state of the controls in the FontPanel // Get the value of the BevelButton and set the // StyledText. If ButtonBold.Value Then Field.StyledText.Bold(Field.SelStart, Field.SelLength) = True Else Field.StyledText.Bold(Field.SelStart, Field.SelLength) = False End If If ButtonItalic.Value Then Field.StyledText.Italic(Field.SelStart, Field.SelLength) = True Else Field.StyledText.Italic(Field.SelStart, Field.SelLength) = False End If If ButtonUnderline.Value Then Field.StyledText.Underline(Field.SelStart, Field.SelLength) = True Else Field.StyledText.Underline(Field.SelStart, Field.SelLength) = False End If Field.styledText.Font(Field.SelStart, Field.SelLength) = FontList.Text Field.styledText.Size(Field.SelStart, Field.SelLength) = val(FontSize.Text) // Find which RadioButton is highlighted and set // the alignment accordingly. // Note that the alignment is set for the paragraph, and // not just the selected text. I could also get a reference // to the paragraph object and change that as well. If Alignment(2).Value Then Field.SelAlignment = EditField.AlignCenter Elseif Alignment(1).Value Then Field.SelAlignment = EditField.AlignRight Else Field.SelAlignment = EditField.AlignLeft End if | Listing 6.112. Function SizeList.CellClick(row as Integer, column as Integer, x as Integer, y as Integer) As Boolean // When a size is selected in the SizeList ListBox // Position the SizeSlider to display the same value. SizeSlider.Value = 144 - val(SizeList.Text) FontSize.Text = str(144-SizeSlider.Value) | Listing 6.113. Sub SizeSlider.ValueChanged() // Set font size according to the // Position of the SizeSlider SizeList.ListIndex = -1 FontSize.Text = str(144-me.Value) | Listing 6.114. Sub PushButton1.Action() // Hide the FontPanel FontPanel.Hide | Listing 6.115. Sub PushButton2.Action() // Apply the currently selected fonts and styles // to the selected text. setCurrentStyle | Listing 6.116. BevelButton1.Sub Action() // Display the platform specific color picker // with the selectColor function and let // the user choose the color of the text Dim c as Color Dim res as Boolean res = selectColor(c, "Select a color") If Res Then Field.SelTextColor = c End If | |