Search code examples
windows-phone-7windows-phone-8

Windows phone Textblock not showing large text


How to show large text string in windows phone using textblock or richtextbox ? currently I am facing issue to show large amount of text in textbox its truncated


Solution

  • To resolve this issue we created a converter. So to start we should place a ContentPresenter on the page instead of TextBlock in order to display content we like

    <ContentPresenter Content="{Binding BodyText,Converter={StaticResource LongTextConverter}}" />
    
    public string BodyText
    

    { get { return "Lorem ipsum dolor sit amet, ..."; } }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            UIElement result = null;
    
            if (value != null)
            {
                string data = value.ToString();
    
                if (data.Length <= MinLength)
                {
                    result = new TextBlock
                                 {
                                     Text = data,
                                     Style = App.Current.Resources["ArticleBodyStyle"] as Style,
                                     HorizontalAlignment = HorizontalAlignment.Stretch
                                 };
                }
                else
                {
                    StackPanel resultPanel = new StackPanel();
                    int min = 0;
                    int max = MinLength;
                    while (true)
                    {
                        string temp = data.Substring(min, max - min);
                        if (max != data.Length)
                        {
                            int index = temp.LastIndexOf('\n');
                            index = index == -1 ? temp.LastIndexOf('.') + 1 : -1;
                            max = (index == -1) ? max : index;
                            temp = data.Substring(min, max) + '\n';
                            resultPanel.Children.Add(new TextBlock
                                                         {
                                                             Margin = new Thickness(12, 0, 12, -30),
                                                             Text = temp,
                                                             Style = App.Current.Resources["ArticleBodyStyle"] as Style,
                                                             HorizontalAlignment = HorizontalAlignment.Stretch
                                                         });
                        }
                        else
                        {
                            resultPanel.Children.Add(new TextBlock
                                                         {
                                                             Margin = new Thickness(12, 0, 12, 0),
                                                             Text = temp,
                                                             Style = App.Current.Resources["ArticleBodyStyle"] as Style,
                                                             HorizontalAlignment = HorizontalAlignment.Stretch
                                                         });
                            break;
                        }
    
                        min += max;
                        max = min + MinLength;
                        if (max > data.Length)
                        {
                            max = data.Length;
                        }
                    }
    
                    result = resultPanel;
                }
            }
    
            return result;
        }