Sto lavorando a un progetto che mi ha fatto approssimare il rendering del testo come immagine e un editor DHTML per il testo. Le immagini vengono renderizzate utilizzando il metodo DrawText di .NET 4 DrawingContext.Calcolo del wrapping di testo in .NET DrawingContext Metodo DrawText
Il metodo DrawText acquisisce il testo insieme alle informazioni sui caratteri e alle dimensioni e calcola il wrapping necessario per far sì che il testo si adatti il più possibile, posizionando un'ellissi alla fine se il testo è troppo lungo. Quindi, se ho il seguente codice per disegnare il testo in un rettangolo che si è abbrevaiate:
string longText = @"A choice of five engines, although the 2-liter turbo diesel, supposedly good for 48 m.p.g. highway, is not coming to America, at least for now. A 300-horsepower supercharged gasoline engine will likely be the first offered in the United States. All models will use start-stop technology, and fuel consumption will decrease by an average of 19 percent across the A6 lineup. A 245-horsepower A6 hybrid was also unveiled, but no decision has yet been made as to its North America sales prospects. Figure later in 2012, if sufficient demand is detected.";
var drawing = new DrawingGroup();
using (var context = drawing.Open())
{
var text = new FormattedText(longText,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface("Calibri"),
30,
Brushes.Green);
text.MaxTextHeight = myRect.Height;
text.MaxTextWidth = myRect.Width;
context.DrawText(text, new Point(0, 0));
}
var db = new DrawingBrush(drawing);
db.Stretch = Stretch.None;
myRect.Fill = db;
C'è un modo per calcolare come il testo sarà avvolto? In questo esempio, il testo in uscita è avvolto ad "2 litri" e "48 m.p.g" ecc come visto nella figura:
* Perfetto *. Stavo guardando attraverso la fonte della struttura e ho visto la tecnica di enumerazione delle linee, ma sembrava che tutto sull'oggetto FormattedText fosse privato; Mi sono avvicinato al solo uso della riflessione per esporre queste proprietà, ma questa è una soluzione molto migliore. A proposito, Ciao Shaun! Molto tempo dopo le cattive strade di La Mirada! – t3rse