简体   繁体   English

Windows Phone 8.1中具有不同颜色的文本的一部分

[英]Part of text with different color in windows phone 8.1

I am creating a windows phone 8.1 app where app provides the user with a text box. 我正在创建Windows Phone 8.1应用程序,其中该应用程序为用户提供了一个文本框。 I want to highlight all the hashtags used in the textbox with different color. 我想用不同的颜色突出显示文本框中使用的所有主题标签。 So as soon as user presses hash(#) on the screen, font color will change till user presses a space key. 因此,只要用户在屏幕上按下hash(#),字体颜色就会改变,直到用户按下空格键为止。 For example, user enters: 例如,用户输入:

This is a #sample statement.

Font color remains black for the part of text "This is a", but as soon as user presses # key, color changes to red (including the hash itself) and all subsequent characters are in red colored font. 文本“ This is a”部分的字体颜色保持黑色,但是一旦用户按#键,颜色就会变为红色(包括哈希本身),并且所有后续字符均为红色字体。 So #sample appears in read color. 因此,#sample以读取颜色显示。 Once user presses a space after the word sample, font color changes back to black and all the remaining text appears to be in black color. 一旦用户在单词样本后面按下空格,字体颜色将变回黑色,而所有剩余的文本将显示为黑色。 How can I achieve this? 我该如何实现? I tried changing the font color but then it changes for the entire text and not just for the hashtag. 我尝试更改字体颜色,但随后更改了整个文本,而不仅是主题标签。

Why not use a RichEditBox? 为什么不使用RichEditBox? Here's something I quickly whipped up: 这是我很快想到的:

<RichEditBox x:Name="tb" TextChanged="tb_TextChanged" />
private void tb_TextChanged(object sender, RoutedEventArgs e)
{
    // we don't want this handler being called as a result of
    // formatting changes being made here
    tb.TextChanged -= tb_TextChanged;

    var doc = tb.Document;
    doc.BatchDisplayUpdates();

    try
    {
        string text;
        doc.GetText(TextGetOptions.None, out text);
        if (text.Length == 0)
            return;

        // check if this word starts with a hash
        var start = doc.Selection.StartPosition - 1;
        while (true)
        {
            if (start < 0 || char.IsWhiteSpace(text[start]))
                return;
            if (text[start] == '#')
                break;
            start--;
        }

        // find the end of the word
        var end = doc.Selection.StartPosition;
        while (start < text.Length && !char.IsWhiteSpace(text[end]))
            end++;

        // set color
        doc.GetRange(start, end).CharacterFormat.ForegroundColor = Colors.RoyalBlue;
    }
    finally
    {
        doc.ApplyDisplayUpdates();
        tb.TextChanged += tb_TextChanged;
    }
}

屏幕截图

You can obviously optimize it more. 您显然可以对其进行更多优化。 It doesn't support formatting pasted text, that's an exercise for you :) 它不支持格式化粘贴的文本,这是您的练习:)

Use this XAML format for text with different color 将此XAML格式用于具有不同颜色的文本

> <TextBlock FontSize="30">
>             <Run Foreground="Red" Text="Hi "></Run>
>             <Run Foreground="Green" Text="This "></Run>
>             <Run Foreground="Blue" Text="is "></Run> 
              <Run Foreground="White" Text="Color."></Run> </TextBlock>

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM