Text styles in localization resources

廉价感情. 提交于 2019-12-08 03:18:42

问题


I've a question about localization resources at windows-phone.

Let's say, I have a string in my Resources file, which should look like as:

This is some text. This value is bold. This one is italic.

And that all stored in a single string field. How could I define such text styles like bold or italic in the Resources itself? I know, I can predefine some custom fonts, like this:

<FontFamily x:Key="CustomBold">...</FontFamily>

and then add is as {StaticResource CustomBold} in page, but that can be a solution if the whole text in string field is bold. And what if I want to make bold a single word in the middle of the phrase?


I want to use native c#-style Resources(i.e. string name -> string value), not writing different implementation.


回答1:


When I needed something like this for iOS, I've implemented very basic BBCode-alike markup language with only a few tags: "[b]", "[/b]", "[[" and "]]" (in my project I didn't even needed the italic, only bold).

However, .NET doesn't have an analog of NSScanner class that I used to parse the syntax. Instead, it has much better support for parsing XML data. So, on WP7 it's easier to implement a very basic subset of XML, with just <b> and <i> tags supported. See the end of this page for sample code.

Here's how to add formatted text pieces into the WP7 TextBlock.

Update: OK, here's the complete solution for you:

[Flags]
enum eParseState: byte
{
    bold = 1,
    italic = 2,
}

// Sample input: "<txt>This is <i>some</i> text. <b>This value is <i>bold</i>.</b> This one is not.</txt>"
static void parseRichText( TextBlock tb, string xml )
{
    tb.Inlines.Clear();
    XmlReader reader = XmlReader.Create( new StringReader( xml ), new XmlReaderSettings() { ConformanceLevel=ConformanceLevel.Fragment } );
    eParseState state = 0;

    var names = new Dictionary<string, eParseState>()
    {
        { "b", eParseState.bold },
        { "i", eParseState.italic },
    };

    Action<bool> actElement = ( bool isOpening ) =>
    {
        string name = reader.Name.ToLower();
        eParseState flag;
        if( !names.TryGetValue( name, out flag ) ) return;
        if( isOpening )
            state |= flag;
        else
            state &= ( ~flag );
    };

    while( reader.Read() )
    {
        switch( reader.NodeType )
        {
            case XmlNodeType.Element:
                actElement( true );
                break;
            case XmlNodeType.EndElement:
                actElement( false );
                break;
            case XmlNodeType.Text:
                var run = new Run() { Text = reader.Value };
                if( 0 != ( state & eParseState.bold ) ) run.FontWeight = FontWeights.Bold;
                if( 0 != ( state & eParseState.italic ) ) run.FontStyle = FontStyles.Italic;
                tb.Inlines.Add( run );
                break;
        }
    }
}



回答2:


Well...

I've found a solution, but it looks quite clumsy.

The control, that should be used is RichTextBox.

In Windows Phone RichTextBox works a bit different, than in native .Net

To set different style for part of the text, we'd have to divide our single string:

So, let's imagine, that we've got a string field in Resources:

This is some text. This value is bold. This one is not.

Dividing it into three strings(yep, that's real clumsy):

  1. This is some text.
  2. This value is bold
  3. This one is not.

Then, in code we're working with RichTextBox rtb.

            Paragraph paragraph = new Paragraph();
            Bold bold = new Bold();
            bold.Inlines.Add(new Run() { Text = _string2FromResources });

            Run run = new Run();
            run.Text = _string1FromResources;
            Run run2 = new Run();
            run2.Text = _string3FromResources;

            paragraph.Inlines.Add(run); 
            paragraph.Inlines.Add(bold);
            paragraph.Inlines.Add(run2);

            rtb.Blocks.Add(paragraph);

That will really work. But the quality of implementation... Well, you got it. So I would really appreciate, if someone can add an example of better solution.



来源:https://stackoverflow.com/questions/13560048/text-styles-in-localization-resources

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