FormattedText 类中的下标/上标?
我正在使用 FormattedText 类创建文本 - 但是在使用此类时如何下标或上标文本?我找到了如何在使用 TextBlock 时执行此操作的解决方案,但我使用的是 FormattedText 而不是 TextBlock ):感谢您的任何提示!
I'm creating Text using the FormattedText class - but how can I subscript oder superscript Text when using this class? I found solution on how to do this when using a TextBlock, but I'm using FormattedText and not the TextBlock ): Thanks for any hint!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
FormattedText
无法执行下标/上标 - 但TextFormatter
可以。TextFormatter
是一个低级 API,您需要编写大量代码才能使用它 - 但大多数代码只是对用于传递格式化参数 intTextFormatter
。如何使用 TextFormatter
TextFormatter
接受一个TextSource
对象并生成多个TextLine
对象(每一行一个),即TextLine.Draw<然后可以使用 /code> 方法将线条绘制到绘图上下文。
TextSource
类是抽象的,您必须对其进行子类化并重写GetTextRun
方法,该方法仅返回位于所提供的字符位置的TextRun
对象。TextRun
也是抽象的 - 但它确实有可以使用的子类 - 有趣的类是TextCharacters
,它包含字符串和格式信息。格式信息位于
TextRunProperties
对象中,不幸的是,这是您必须子类化的另一个抽象类。TextRunProperties
具有TextRunTypographyProperties
类型的TypographyProperties
属性。TextRunTypographyProperties
是您需要子类化的另一个抽象类。最后,
TextRunTypographyProperties
有Variants
属性,您可以像 TextBlock 示例一样使用。代码示例
下面是我可以编写的用于绘制上标文本的最少代码:
首先,我们的 TextRunProperties 和 TextRunTypographyProperties 可以返回上标字体变体:
以及用于段落格式设置的类似类(取自 MSDN TextFormatter 示例):
现在 TextSource 实现:
所有这些就是剩下要做的就是初始化 CustomTextSource 并绘制文本:
就是这样 - 我们在绘图上下文中有上标文本。
FormattedText
can't do subscript/superscript - butTextFormatter
can.TextFormatter
is a low level API, you need to write a lot of code to use it - but most of the code is just subclassing all the classes used to pass formatting parameters intTextFormatter
.How to use TextFormatter
TextFormatter
takes aTextSource
object and produces multipleTextLine
objects (one for each line), theTextLine.Draw
method can then be used to draw the line to a drawing context.The
TextSource
class is abstract, you have to subclass it and override theGetTextRun
method that simply returns aTextRun
object that's in the character position provided.TextRun
is also abstract - but it does have subclasses you can use - the interesting class isTextCharacters
that contains a string and formatting information.The formatting information is in a
TextRunProperties
object, unfortunately, this is another abstract class you have to subclass.TextRunProperties
has aTypographyProperties
property of typeTextRunTypographyProperties
.TextRunTypographyProperties
is yet another abstract class you need to subclass.And finally
TextRunTypographyProperties
has theVariants
property you can use like the TextBlock example.Code Example
Here is the minimal code I could write to draw superscript text:
First, Our TextRunProperties and TextRunTypographyProperties that can return superscript font variant:
And a similar class for paragraph formatting (takes from MSDN TextFormatter sample):
Now TextSource implementation:
And all that's left to do is initialize out CustomTextSource and draw the text:
And that's it - we have superscript text in a drawing context.
根本没有办法使用
FormattedText
,请参阅此处的完整参考:您可以使用它
TextBlock
,正如您已经发现的:或者您可以简单地将
FormattedText
变小 (SetFontSize
) 并手动将其放置在上方/下方。如果您可以提供更多有关您使用
FormattedText
的情况、或者您试图解决的问题、或者为什么不能使用TextBlock
的更多背景信息,请发布对此答案的回复,我们将很乐意提供更具体的示例帮助。希望有帮助!
There simply isn't a way with
FormattedText
, see the complete reference here:You can do with it
TextBlock
, as you have already discovered:Or you could simply make the
FormattedText
smaller (SetFontSize
) and position it above/below manually.If you could give some more context as to the situation you are using the
FormattedText
, or the problem you are trying to solve, or why you cannot use aTextBlock
, please post a reply to this answer and we will be happy to help with more specific examples.Hope that helps!