Get/set height of Run inside a paragraph in UWP

岁酱吖の 提交于 2020-01-16 08:42:45

问题


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.

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.

Is there a way to find and set the height of text elements inside a TextBlock?

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.


回答1:


The height of Run is determined by FontSize and 1/2 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.

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).

If an article has multiple paragraphs, you can consider using multiple TextBlocks to calculate the height of each paragraph individually.

If you want to select text across controls, you can also consider converting the text to HTML and using WebView for display.

Best regards.



来源:https://stackoverflow.com/questions/59573045/get-set-height-of-run-inside-a-paragraph-in-uwp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!