在 UWP 中获取/设置段落内运行的高度 [英] Get/set height of Run inside a paragraph in UWP
问题描述
我需要将多个 Run
元素添加到 TextBlock
.每个Run
段落必须在同一个TextBlock
内(允许选择多个段落).但是,我还需要找到每个Run
的高度,并在必要时设置高度.
I need to add multiple Run
elements to a TextBlock
. Each Run
paragraph must be within the same TextBlock
(to allow for selecting multiple paragraphs). However, I also need to find the height of each Run
, and set the height if necessary.
因为 Run
不继承自 FrameworkElement
,Height
和 ActualHeight
不是可用的属性.另一方面,Run
只允许可以进入 InlineCollection
(不包括从 FrameworkElement
继承的任何内容)的子元素.这使得查找和设置 Run
(或 TextBlock
,就此而言)的任何子项的高度变得不可能.
Because Run
does not inherit from FrameworkElement
, Height
and ActualHeight
are not available properties. On the other hand, Run
only allows children that can go in an InlineCollection
(which does not include anything inheriting from FrameworkElement
). This makes finding and setting the height of any child of a Run
(or TextBlock
, for that matter) impossible.
有没有办法在 TextBlock
中查找和设置文本元素的高度?
Is there a way to find and set the height of text elements inside a TextBlock
?
请注意,我更愿意直接使用 XAML/C# 来提高速度,而不是仅仅因为 XAML 不让人们做一些明显的事情(用光标选择多个段落)就必须在 HTML/JavaScript 上标记.也有人建议使用行高或字体大小,但这些都达不到相同的设计效果.
As a note, I'd much rather use straight XAML/C# for its speed than have to tag on HTML/JavaScript just because XAML doesn't let one do something obvious (select multiple paragraphs with the cursor). It has also been suggested to use line heights or font size, but these don't achieve the same design effect.
推荐答案
Run
的高度由FontSize
和1/2 LineHeight
.
虽然Run
没有提供类似Height
的属性来设置它的高度,但是你可以通过修改Run
变相修改Run
的高度code>Run.FontSize 或 TextBlock.LineHeight
.
Although Run
does not provide a Height
like property to set its height, you can modify Run
's height in disguise by modifying Run.FontSize
or TextBlock.LineHeight
.
但是对于文本换行,计算单个 Run 的高度并不容易.我们还必须考虑FontFamily
(因为等宽字体和非等宽字体在相同的字体大小下表现不同).
But for text wrapping, calculating the height of a single Run is not easy. We must also consider FontFamily
(because monospace fonts and non-monospace fonts behave differently under the same font size).
如果一篇文章有多个段落,可以考虑使用多个TextBlock
来分别计算每个段落的高度.
If an article has multiple paragraphs, you can consider using multiple TextBlock
s to calculate the height of each paragraph individually.
如果要跨控件选择文本,还可以考虑将文本转换为 HTML 并使用 WebView
进行显示.
If you want to select text across controls, you can also consider converting the text to HTML and using WebView
for display.
最好的问候.
这篇关于在 UWP 中获取/设置段落内运行的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!