关于Squeak中设置TextMorph旋转的选择器及初始化调用的技术问询
Hey there! Great question—let's break down how to handle TextMorph rotation, especially during initialization, and the key selectors you need to know.
Core Selector for Rotation
First off, the main selector you’re looking for to set rotation on any Morph (including TextMorph) is rotationDegrees:. This method takes a numeric value representing the angle in degrees (positive values rotate clockwise, negative values rotate counter-clockwise) and updates the Morph’s transformation matrix to apply the rotation.
If you prefer working with radians instead, you can use rotationRadians:—it does the exact same job but accepts radian values instead of degrees.
Calling Rotation During Initialization
To apply rotation right when your TextMorph is initialized, you have two simple, effective options:
Option 1: Subclass TextMorph and Override initialize
Create a custom subclass of TextMorph where you add rotation logic to the initialization method. This is ideal if you need reusable rotated text morphs:
"First define the subclass" TextMorph subclass: #RotatedTextMorph instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Your-Custom-Category' "Override the initialize method" RotatedTextMorph>>initialize super initialize. "Always call the superclass's initialize first!" self rotationDegrees: 45. "Set your desired rotation angle here" self contents: 'I''m rotated 45 degrees!' "Add your text content"
Then create an instance with RotatedTextMorph new openInWorld.
Option 2: Chain Calls When Creating a TextMorph Instance
If you don’t want to subclass, just call the rotation selector immediately after creating the TextMorph:
| myRotatedText | myRotatedText := TextMorph new contents: 'Rotated 90 degrees clockwise'; rotationDegrees: 90; yourself. myRotatedText openInWorld.
Quick Display Tip
If you notice layout glitches (like text not rendering correctly within bounds) after setting rotation, call myRotatedText layoutChanged or myRotatedText fullBounds to force the Morph to recalculate its layout and redraw properly.
The rotation tool you use via the Morphic halo menu uses these same selectors under the hood—so this is the official, supported way to handle rotation programmatically.
内容的提问来源于stack exchange,提问作者Gregg Arthur Evans

