Flex: modifica dell'area di testo
-
10-07-2019 - |
Domanda
Sto creando un editor di testo utilizzando Area di testo. Quale utente può modificare la dimensione del carattere, la famiglia e così via
Questo è il mio codice come:
private function ChangeFont(event: Event):void
{
var mySelectedTextRange:TextRange = new TextRange(thistxtarea,true,
thistxtarea.selectionBeginIndex,
thistxtarea.selectionEndIndex);
mySelectedTextRange.fontSize = int(cmbbxFntSze.text);
thistxtarea.setFocus();
}
Ho questa casella combinata per inserire la dimensione del carattere desiderata:
<mx:ComboBox x="78" y="8" width="114" id="cmbbxFntFam" close="ChangeFont(event)"></mx:ComboBox>
Come posso modificare le proprietà del carattere se il testo all'interno non è evidenziato? Ad esempio, posiziono il puntatore del mouse sull'ultimo indice di testo all'interno della mia area di testo e seleziono nella casella combinata la dimensione del carattere desiderata. La seguente dimensione del carattere della lettera immessa nell'area di testo dovrebbe essere la dimensione del carattere selezionata nella casella combinata. Il codice che pubblico funziona solo se metto in evidenza il testo desiderato.
Soluzione
Questo serve per impostare lo stile
private function setTextStyles(type:String, value:Object = null):void
{
if(thisindex != -1)
{
var tf:TextFormat;
var beginIndex:int = textArea.getTextField().selectionBeginIndex;
var endIndex:int = textArea.getTextField().selectionEndIndex;
textArea.getTextField().alwaysShowSelection = true;
if (beginIndex == endIndex)
{
tf = previousTextFormat;
}
else
tf = new TextFormat();
if (type == "bold" || type == "italic" || type == "underline")
{
tf[type] = value;
}
else if (type == "align")
{
if (beginIndex == endIndex)
{
tf = new TextFormat();
}
// Apply the paragraph styles to the whole paragraph instead of just
// the selected text
beginIndex = textArea.getTextField().getFirstCharInParagraph(beginIndex) - 1;
beginIndex = Math.max(0, beginIndex);
endIndex = textArea.getTextField().getFirstCharInParagraph(endIndex) +
textArea.getTextField().getParagraphLength(endIndex) - 1;
tf[type] = value;
previousTextFormat[type] = value;
if (!endIndex)
textArea.getTextField().defaultTextFormat = tf;
}
else if (type == "font")
{
tf[type] = cmbbxFntFam.text;
}
else if (type == "size")
{
var fontSize:uint = uint(cmbbxFntSze.text);
if (fontSize > 0)
tf[type] = fontSize;
}
else if (type == "color")
{
tf[type] = uint(clrpckerFontColor.selectedColor);
}
textFormatChanged = true;
if (beginIndex == endIndex)
{
previousTextFormat = tf;
}
else
{
textArea.getTextField().setTextFormat(tf,beginIndex,endIndex);//textArea.setTextFormat(tf,beginIndex,endIndex);
}
dispatchEvent(new Event("change"));
var caretIndex:int = textArea.getTextField().caretIndex;
var lineIndex:int = textArea.getTextField().getLineIndexOfChar(caretIndex);
textArea.invalidateDisplayList();
textArea.validateDisplayList();
textArea.validateNow();
// Scroll to make the line containing the caret under viewable area
while (lineIndex >= textArea.getTextField().bottomScrollV)
{
textArea.verticalScrollPosition++;
}
callLater(textArea.setFocus);
}
}
Questo codice viene utilizzato per ottenere lo stile da textArea
private function getTextStyles():void
{
if (!textArea)
return;
var tf:TextFormat;
var beginIndex:int = textArea.getTextField().selectionBeginIndex;
var endIndex:int = textArea.getTextField().selectionEndIndex;
if (textFormatChanged)
previousTextFormat = null;
if (beginIndex == endIndex)
{
tf = textArea.getTextField().defaultTextFormat;
if (tf.url != "")
{
var carIndex:int = textArea.getTextField().caretIndex;
if (carIndex < textArea.getTextField().length)
{
var tfNext:TextFormat=textArea.getTextField().getTextFormat(carIndex, carIndex + 1);
if (!tfNext.url || tfNext.url == "")
tf.url = tf.target = "";
}
else
tf.url = tf.target = "";
}
}
else
tf = textArea.getTextField().getTextFormat(beginIndex,endIndex);
if (cmbbxFntSze.text != tf.font)
setComboSelection(cmbbxFntFam, tf.font);
if (int(cmbbxFntSze.text) != tf.size)
setComboSelection(cmbbxFntSze,String(tf.size));
if (clrpckerFontColor.selectedColor != tf.color)
clrpckerFontColor.selectedColor = Number(tf.color);
if (btnBold.selected != tf.bold)
btnBold.selected = tf.bold;//Alert.show("bold");
if (btnItalic.selected != tf.italic)
btnItalic.selected = tf.italic;
if (btnUnderline.selected != tf.underline)
btnUnderline.selected = tf.underline;
if (tf.align == "left")
alignButtons.selectedIndex = 0;
else if (tf.align == "center")
alignButtons.selectedIndex = 1;
else if (tf.align == "right")
alignButtons.selectedIndex = 2;
else if (tf.align == "justify")
alignButtons.selectedIndex = 3;
if (textArea.getTextField().defaultTextFormat != tf)
textArea.getTextField().defaultTextFormat = tf;
previousTextFormat = tf;
textFormatChanged = false;
lastCaretIndex = textArea.getTextField().caretIndex;
thishtmltxt = textArea.htmlText;
textArea.validateNow();
}
Verifica la presenza di errori minori, quindi quando scrivo questo ho alcune tracce commentate
Altri suggerimenti
Hai forse dato un'occhiata a come mx.controls.RichTextEditor fa questo? Puoi trovarlo in ... \ frameworks \ progetti \ framework \ src \ mx \ controls
Se esegui la scansione di quel codice, vedrai che RichTextEditor mantiene le impostazioni correnti dello stile di testo nella variabile TextFormat che mantiene, quindi applica quello stile al testo appena inserito. Tale variabile viene aggiornata quando l'utente cambia carattere / dimensione o quando la selezione cambia per afferrare lo stile adiacente. Vi è una considerazione speciale anche nel caso di selectionBeginIndex == selectionEndIndex.