Foreground colour of textblock based on position of part of the letter

社会主义新天地 提交于 2019-12-12 03:35:52

问题


A Mars bar challenge at work is that using Xaml only, make the following text of a text block gray by default. But, from half way through the letter "e" and to half way to last character "1" make the colour red.

The text is "Item 1". So, "I" and "t" all gray, but the first half of "e" red, all of "m" red and then half of "1" red and the last half of "1" gray.

I have no idea about this one.


回答1:


Were you thinking about something like this?

<TextBlock FontSize="72">
    <Run Foreground="Gray" Text="It" /><Run Text="e">
        <Run.Foreground>
            <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
                <GradientStop Color="Gray" Offset="0"/>
                <GradientStop Color="Gray" Offset="0.5" />
                <GradientStop Color="Red" Offset="0.5" />
                <GradientStop Color="Red" Offset="1" />
            </LinearGradientBrush>
        </Run.Foreground>
    </Run><Run Text="m" Foreground="Red" /><Run Text=" 1">
        <Run.Foreground>
            <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
                <GradientStop Color="Red" Offset="0"/>
                <GradientStop Color="Red" Offset="0.5" />
                <GradientStop Color="Gray" Offset="0.5" />
                <GradientStop Color="Gray" Offset="1" />
            </LinearGradientBrush>
        </Run.Foreground>
    </Run>
</TextBlock>

Result:



来源:https://stackoverflow.com/questions/35682023/foreground-colour-of-textblock-based-on-position-of-part-of-the-letter

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