是否有可能seletively颜色的Silverlight / WPF包裹的TextBlock有可能、包裹、颜色、Silverlight

2023-09-04 10:31:24 作者:安靜的離開了

举例来说,如果我有一个TextBlock:

For instance, if I have a TextBlock:

Lorem存有悲坐阿梅德,consectetur adipisicing ELIT,sed的做eiusmod tempor incididunt UT labore等dolore麦格纳aliqua。UT斯达康enim广告微量veniam,QUIS nostrud实习ullamco laboris妮斯UT aliquip前EA commodo consequat。DUIS奥特irure在悲再prehenderit在voluptate velit ESSE cillum dolore欧盟fugiat法无pariatur。Excepteur SINT occaecat cupidatat非proident,必须遵守的过失魁正式开通了deserunt mollit阿尼姆ID EST laborum。

"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

我想要这个TextBlock的,使得字体颜色应该是红色的第一10%,其余的是绿色

I want the first 10% of this TextBlock, such that the font color should be red, and the rest should be green.

这也许意味着它将颜色的Lorem存有悲坐阿梅德,consectetur adipisici和N的一部分。基本上,逐像素的字体颜色,而不是性格明智的。

This perhaps means it would color the "Lorem ipsum dolor sit amet, consectetur adipisici", and part of the "n". Basically pixel-wise font coloring instead of character-wise.

另外一个重要的行为是个尊重的包装,因为如果TextBlock的内容50%是彩色的,它应该是指文本的50%,阅读顺序是彩色的,而不是第一个块的一半。

Another important behavior is that the percentage respects the wrapping, in that if 50% of the TextBlock contents is colored, it should mean 50% of the text in reading order is colored, and not the first half of the block.

例如,这是一个类似的问题(与正确答案),但对于标签/的TextBlocks无包装: http://stackoverflow.com/questions/2394421/is-it-possible-to-seletively-color-a-label-in-silverlight

For example, this is a similar question (with correct answers), but regarding Label/TextBlocks with no wrapping: http://stackoverflow.com/questions/2394421/is-it-possible-to-seletively-color-a-label-in-silverlight

推荐答案

我不相信这是可能的逐像素的(至少在没有测量文本和使用的技术之一,从链接的问题上线 - 通过线的基础上)。它可以使用运行对象来完成的性格明智的:

I don't believe this is possible pixel-wise (at least not without measuring the text and using one of the techniques from the linked question on a line-by-line basis). It can be done character-wise using a Run object:

<TextBlock FontSize="24" TextWrapping="Wrap">
  <Run Foreground="Red">Lorem ipsum dolor sit amet, consectetur</Run>
  adipisicing elit, sed do eiusmod tempor incididunt ut labore et
  dolore magna aliqua. Ut enim ad minim veniam...
</TextBlock>

如果文字是动态的,你需要写code分区文成两个运行(并将它们分配给TextBlock.Inlines集合),但它不应该太硬来封装成自定义的控制。

If the text is dynamic, you'd need to write code to partition the text into the two Runs (and assign them to the TextBlock.Inlines collection), but it shouldn't be too hard to encapsulate that into a custom control.