WPF 文本呈现(2)
生活随笔
收集整理的這篇文章主要介紹了
WPF 文本呈现(2)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?
文本輸出規(guī)則
比如輸出Hello,可以多種輸出方法.如下
當都遵循一個原則,字符將從左到右輸出,
如H(0,1),ell(1,3),o(4,1) 表示從索引值輸出多少個字符.這個過程將一直持續(xù)下去直到段落結(jié)束為止
輸出多彩文字
public override TextRun GetTextRun(int textSourceCharacterIndex) {//typeof(Brushes).GetProperties()[0].GetValue()if (textSourceCharacterIndex >= _text.Length){return new TextEndOfParagraph(1);}_currentRendering.TextColor = brushs[textSourceCharacterIndex] as SolidColorBrush;// Create TextCharacters using the current font rendering properties.if (textSourceCharacterIndex < _text.Length){return new TextCharacters(_text,textSourceCharacterIndex,1,new GenericTextRunProperties(_currentRendering));}// Return an end-of-paragraph if no more text source.return new TextEndOfParagraph(1); }輸出結(jié)果
每次從當前索引輸出一個字符,每次更改字符屬性的顏色
更改大小
每隔三次字符輸出Winow單詞
_currentRendering.TextColor = brushs[textSourceCharacterIndex] as SolidColorBrush;_currentRendering.FontSize = textSourceCharacterIndex*12+12;return new TextCharacters(_text,textSourceCharacterIndex,3,new GenericTextRunProperties(_currentRendering));結(jié)束段落
告訴段落結(jié)束的辦法是在GetTextRun方法中返回TextEndOfParagraph,比如默認當索引字符等于輸出字符長度時則認為段落結(jié)束if (textSourceCharacterIndex > _text.Length) {return new TextEndOfParagraph(1); }
在段落添加額外的文字,在輸出文字結(jié)束以后添加段落末尾的文字,如下圖EndTextWord可以以自行的邏輯添加
var customEndText = "EndTextWord"; var gap = textSourceCharacterIndex - _text.Length; if (gap >= 0 && gap < customEndText.Length) {_currentRendering.FontSize = 24;return new TextCharacters(customEndText,0,customEndText.Length,new GenericTextRunProperties(_currentRendering)); } if (gap >= customEndText.Length) {return new TextEndOfParagraph(1); }多段落
如下文字
若窗體寬度不夠的話,將換行顯示
在TextLine調(diào)用Draw方法以后,若寬度不夠,將只呈現(xiàn)部分文字,并有一個Length屬性,然后可以根據(jù)Length屬性繼續(xù)呈現(xiàn)第二段文字.如下代碼(僅做示例而用)
TextLine myTextLine = formatter.FormatLine(textStore,0,80,new GenericTextParagraphProperties(currentRendering),null); myTextLine.Draw(dc, new Point(0, 0), InvertAxes.None); myTextLine.Dispose(); // Draw second paragraph. using (TextLine myTextLine2 = formatter.FormatLine(textStore,myTextLine.Length,80,new GenericTextParagraphProperties(currentRendering),null)) {myTextLine2.Draw(dc, new Point(0, 25), InvertAxes.None); }現(xiàn)在
轉(zhuǎn)載于:https://www.cnblogs.com/Clingingboy/archive/2010/12/03/1895460.html
總結(jié)
以上是生活随笔為你收集整理的WPF 文本呈现(2)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基础篇:事件的发送和处理
- 下一篇: 错误 SQL TOP子句中的行数必须是