Inheritance: Span, IUnderline
示例#1
0
        public static void OnTextContent(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var richText = (RichTextBlock)d;
            var textContent = (string)e.NewValue;

            richText.Blocks.Clear();

            if(string.IsNullOrEmpty(textContent)) {
                return;
            }
            var paragraph = new Paragraph();
            richText.Blocks.Add(paragraph);

            var matches = UrlRegex.Matches(textContent);
            if(matches.Count == 0) {
                paragraph.Inlines.Add(new Run { Text = textContent });
                return;
            }
            int index = 0;
            foreach(Match match in matches) {
                Uri uri = null;
                Uri.TryCreate(match.Value, UriKind.Absolute, out uri);
                if(match.Index > 0) {
                    var length = match.Index - index;
                    if(length > 0) {
                        paragraph.Inlines.Add(new Run { Text = textContent.Substring(index, length) });
                    }
                }

                var underline = new Underline();
                underline.Inlines.Add(new Run { Text = uri.Host });
                var linkContent = new TextBlock();
                linkContent.Inlines.Add(underline);
                var hyperlink = new HyperlinkButton {
                    NavigateUri = uri,
                    Content = linkContent,
                    Style = (Style)Application.Current.Resources["TextButtonStyle"],
                    Margin = new Thickness(0, 0, 0, -4)
                };
                paragraph.Inlines.Add(
                    new InlineUIContainer {
                        Child = hyperlink
                    });
                index = match.Index + match.Length;
            }
            if(index < textContent.Length - 1) {
                var lastRunText = textContent.Substring(index);
                if(lastRunText.Length > 0) {
                    paragraph.Inlines.Add(new Run { Text = lastRunText });
                }
            }
        }
        public MainPage()
        {
            this.InitializeComponent();

            var underline = new Underline();
            var run = new Run();
            run.Text = "コードビハインドから下線を追加";

            underline.Inlines.Add(run);

            this.textBlock.Inlines.Add(underline);

            //this.DataContext = new MainPageViewModel() { underlineText = "アンダーラインを引きたい場合" };
            this.DataContext = new MainPageViewModel() { normalText = "アンダーラインを引きたくない場合" };
        }
        private void OnUnderlineAppBarCheckBoxChecked(object sender, RoutedEventArgs e) {
            CheckBox chkbox = sender as CheckBox;
            Inline inline = txtblk.Inlines[0];

            if ((bool)chkbox.IsChecked && !(inline is Underline)) {
                Underline underline = new Underline();
                txtblk.Inlines[0] = underline;
                underline.Inlines.Add(inline);
            }
            else if (!(bool)chkbox.IsChecked && inline is Underline) {
                Underline underline = inline as Underline;
                Run run = underline.Inlines[0] as Run;
                underline.Inlines.Clear();
                txtblk.Inlines[0] = run;
            }
        }
示例#4
0
        private static Paragraph ParseForRichTextParagraph(string message)
        {
            var ret = new Paragraph();

            var words = message.Split(" ".ToCharArray());
            foreach (var word in words)
            {
                if (word.Contains("@"))
                    ret.Inlines.Add(new Run { Text = word + " ", Foreground = greenBrush });
                else if (word.Contains("#"))
                    ret.Inlines.Add(new Run { Text = word + " ", Foreground = orangeBrush });
                else if (word.Contains("http"))
                {
                    var ul = new Underline();
                    ul.Inlines.Add(new Run { Text = word + " ", Foreground = blueBrush });
                    ret.Inlines.Add(ul);
                }
                else
                    ret.Inlines.Add(new Run { Text = word + " " });
            }

            return ret;
        }
示例#5
0
 private static Inline GenerateUnderline(HtmlNode node)
 {
     Underline u = new Underline();
     AddChildren(u, node);
     return u;
 }
        public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context)
        {
            var parentContainer = (parent as RichTextBlockContainer).Get();

            Span span = new Span();
            Underline underline = null;

            var textDecoration = element.Style("text-decoration");
            if (!string.IsNullOrEmpty(textDecoration))
            {
                if (textDecoration == "underline")
                {
                    underline = new Underline();
                    underline.Inlines.Add(span);
                }
            }

            var color = element.Style("color");
            if (string.IsNullOrEmpty(color) == false)
            {
                color = color.Trim().TrimStart('#');
                if (color.Length == 6)
                {
                    var sR = color.Substring(0, 2);
                    var sG = color.Substring(2, 2);
                    var sB = color.Substring(4, 2);
                    byte r, g, b;
                    if (byte.TryParse(sR, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out r)
                        && byte.TryParse(sG, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out g)
                        && byte.TryParse(sB, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out b))
                    {
                        var value = Color.FromArgb(255, r, g, b);
                        if (value != Colors.White && value != Colors.Black)
                        {
                            if (underline != null)
                            {
                                underline.Foreground = new SolidColorBrush(value);
                            }
                            else
                            {
                                span.Foreground = new SolidColorBrush(value);
                            }
                        }
                    }
                }
            }

            var fontSize = element.Style("font-size");
            if (!string.IsNullOrEmpty(fontSize))
            {
                fontSize = fontSize.Replace("px", "");
                if (underline != null)
                {
                    underline.FontSize = double.Parse(fontSize);
                }
                else
                {
                    span.FontSize = double.Parse(fontSize);
                }
            }

            var fontWeight = element.Style("font-weight");
            if (!string.IsNullOrEmpty(fontWeight))
            {
                if (underline != null)
                {
                    underline.FontWeight = FontWeights.Bold;
                }
                else
                {
                    span.FontWeight = FontWeights.Bold;
                }
            }


            var textAlign = element.Style("text-align");
            if (!string.IsNullOrEmpty(textAlign))
            {
                if (textAlign == "left")
                {
                    parentContainer.TextAlignment = Windows.UI.Xaml.TextAlignment.Left;
                }
                else if (textAlign == "right")
                {
                    parentContainer.TextAlignment = Windows.UI.Xaml.TextAlignment.Right;
                }
                else
                {
                    parentContainer.TextAlignment = Windows.UI.Xaml.TextAlignment.Center;
                }
            }

            var textStyle = element.Style("text-style");
            if (!string.IsNullOrEmpty(textStyle))
            {
                if (textStyle == "italic")
                {
                    if (underline != null)
                    {
                        underline.FontStyle = FontStyle.Italic;
                    }
                    else
                    {
                        span.FontStyle = FontStyle.Italic;
                    }
                }
            }

            if (underline != null)
            {
                parent.Add(underline);
                context.RenderNode(element, new SpanContainer(underline));
            }
            else
            {
                parent.Add(span);
                context.RenderNode(element, new SpanContainer(span));
            }
        }
 /// <summary>
 ///     Configures the dialog to act as a lockscreen.
 /// </summary>
 private void SetupLocked()
 {
     PlatformAdapter.SendToCustomLogger("PincodeDialog.SetupCreate - Configuring the dialog to act as a lockscreen", LoggingLevel.Information);
     Title.Text = LocalizedStrings.GetString("passcode_enter_code_title");
     ContentFooter.Text = LocalizedStrings.GetString("passcode_confirm");
     Description.Text = "";
     // code needed to "underline" the text
     var description = new Run();
     var underline = new Underline();
     description.Text = LocalizedStrings.GetString("passcode_forgot_passcode");
     underline.Inlines.Add(description);
     Description.Inlines.Add(underline);
     Passcode.KeyDown += LockedClick;
     Description.Tapped += Description_Tapped;
     RetryCounter = MaximumRetries;
 }
        private object getFormattedText(string text)
        {
            Inline runResult;
            var run = new Run()
            {
                Text = text,
                FontFamily = CurrentFont,
                Foreground = new SolidColorBrush(CurrentTextColor),
                FontWeight = IsBold ? FontWeights.Bold : FontWeights.Normal,
                FontStyle = IsItalic ? FontStyle.Italic : FontStyle.Normal,
            };
            if (CurrentFontSize > 0)
                run.FontSize = CurrentFontSize;
            runResult = run;
            if (IsUnderline)
            {
                Span underline = new Underline();
                underline.Inlines.Add(run);
                runResult = underline;
            }


            if (!(rootPanel.Background is SolidColorBrush) || CurrentBGColor != ((SolidColorBrush) rootPanel.Background).Color)
            {
                var tb = new TextBlock();
                tb.TextWrapping = TextWrapping.WrapWholeWords;
                tb.Inlines.Add(runResult);

                var border = new Border();
                border.Background = new SolidColorBrush(CurrentBGColor);
                border.Child = tb;

                ////if(IsStrikethrough)
                ////{
                ////    var b = new Border();
                ////    b.BorderThickness = new Thickness(0,1,0,0);
                ////    b.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch;
                ////    b.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center;
                ////    b.BorderBrush = new SolidColorBrush(CurrentTextColor);
                ////    innerGrid.Children.Add(b);
                ////}

                return border;
            }
            return (Inline) runResult;
        }
示例#9
0
 private static Span RenderTextContent(TextModel text)
 {
     if (text.TextType == TextType.Bold)
     {
         var span = new Bold();
         if (!string.IsNullOrWhiteSpace(text.Text))
             span.Inlines.Add(new Run()
             {
                 Text = text.Text
             });
         AddInlineChildren(span, text);
         foreach (var inline in span.Inlines)
         {
             inline.FontWeight = FontWeights.Bold;
         }
         return span;
     }
     if (text.TextType == TextType.Cursive)
     {
         var span = new Span();
         if (!string.IsNullOrWhiteSpace(text.Text))
             span.Inlines.Add(new Run()
             {
                 Text = text.Text
             });
         AddInlineChildren(span, text);
         foreach (var inline in span.Inlines)
         {
             inline.FontStyle = FontStyle.Italic;
         }
         return span;
     }
     if (text.TextType == TextType.Hyperlink)
     {
         var span = new Hyperlink()
         {
             NavigateUri = new Uri(text.Text)
         };
         AddInlineChildren(span, text);
         return span;
     }
     if (text.TextType == TextType.Underline)
     {
         var span = new Underline();
         if (!string.IsNullOrWhiteSpace(text.Text))
             span.Inlines.Add(new Run()
             {
                 Text = text.Text
             });
         AddInlineChildren(span, text);
         return span;
     }
     else//(text.TextType == TextType.Normal)
     {
         var span = new Span();
         span.Inlines.Add(
             new Run()
             {
                 Text = text.Text
             });
         AddInlineChildren(span, text);
         return span;
     }
 }
        private static void ParseElement(XmlElement element, ITextContainer parent)
        {
            foreach (var child in element.ChildNodes)
            {
                if (child is Windows.Data.Xml.Dom.XmlText)
                {
                    if (string.IsNullOrEmpty(child.InnerText) ||
                        child.InnerText == "\n")
                    {
                        continue;
                    }

                    parent.Add(new Run { Text = child.InnerText });
                }
                else if (child is XmlElement)
                {
                    XmlElement e = (XmlElement)child;
                    switch (e.TagName.ToUpper())
                    {
                        case "P":
                            var paragraph = new Paragraph();
                            parent.Add(paragraph);
                            ParseElement(e, new ParagraphTextContainer(paragraph));
                            break;
                        case "STRONG":
                            var bold = new Bold();
                            parent.Add(bold);
                            ParseElement(e, new SpanTextContainer(bold));
                            break;
                        case "U":
                            var underline = new Underline();
                            parent.Add(underline);
                            ParseElement(e, new SpanTextContainer(underline));
                            break;
                        case "A":
                            ParseElement(e, parent);
                            break;
                        case "BR":
                            parent.Add(new LineBreak());
                            break;
                    }
                }

            }
        }
示例#11
0
        private static void ParseElement(XmlElement element, ITextContainer parent)
        {
            foreach (var child in element.ChildNodes)
            {
                var tagNameToUpper = (child as XmlElement)?.TagName?.ToUpper();

                if (!isInP
                    && !(child is XmlElement && (tagNameToUpper == "P" || tagNameToUpper == "SPAN" || tagNameToUpper == "DIV" || tagNameToUpper == "IMG")))
                {
                    var paragraph = new Paragraph();
                    parent.Add(paragraph);
                    isInP = true;
                    parent = new ParagraphTextContainer(parent, paragraph);
                }

                if (child is XmlText)
                {
                    var text = child.InnerText.Replace("\n", "").Trim();
                    if (string.IsNullOrEmpty(text))
                        continue;

                    text = text.Replace("</form>", "");
                    parent.Add(new Run { Text = System.Net.WebUtility.HtmlDecode(text), Foreground = new SolidColorBrush(Windows.UI.Colors.Black) });
                }
                else if (child is XmlElement)
                {
                    XmlElement e = (XmlElement)child;
                    switch (tagNameToUpper)
                    {
                        case "P":
                        case "DIV":
                        case "SPAN":
                            if(tagNameToUpper == "DIV")
                            {
                                var classVal = e.Attributes.FirstOrDefault(a => a.NodeName == "class")?.NodeValue?.ToString() ?? "";
                                
                                if(classVal.Contains("open-list-comments") ||
                                   classVal.Contains("open-list-footer") ||
                                   classVal.Contains("modal-body"))
                                {
                                    return;
                                }
                            }

                            if (isInP)
                            {
                                if (tagNameToUpper == "P")
                                    parent.Add(new LineBreak());
                                ParseElement(e, parent);
                            }
                            else
                            {
                                var paragraph = new Paragraph();
                                parent.Add(paragraph);
                                isInP = true;
                                ParseElement(e, new ParagraphTextContainer(parent, paragraph));
                                isInP = false;
                            }
                            break;
                        case "B":
                        case "STRONG":
                        case "H1":
                        case "H2":
                        case "H3":
                        case "H4":
                        case "H5":
                        case "H6":
                            var bold = new Bold();
                            parent.Add(bold);
                            ParseElement(e, new SpanTextContainer(parent, bold));
                            break;
                        case "I":
                        case "EM":
                            var italic = new Italic();
                            parent.Add(italic);
                            ParseElement(e, new SpanTextContainer(parent, italic));
                            break;
                        case "U":
                            var underline = new Underline();
                            parent.Add(underline);
                            ParseElement(e, new SpanTextContainer(parent, underline));
                            break;
                        case "A":
                            var url = e.Attributes.FirstOrDefault(a => a.NodeName == "href")?.NodeValue;
                            Uri uri;
                            if (url != null && Uri.TryCreate(url.ToString(), UriKind.Absolute, out uri))
                            {
                                var link = new Hyperlink();
                                link.NavigateUri = uri;
                                parent.Add(link);
                                ParseElement(e, new SpanTextContainer(parent, link));
                            }
                            else
                            {
                                ParseElement(e, parent);
                            }

                            break;
                        case "BR":
                            parent.Add(new LineBreak());
                            break;
                        case "IMG":
                            var src = e.Attributes.FirstOrDefault(a => a.NodeName == "src")?.NodeValue;
                            Uri srcUri;
                            if (src != null && Uri.TryCreate(src.ToString(), UriKind.Absolute, out srcUri))
                            {
                                var img = new InlineUIContainer
                                {
                                    Child = new Windows.UI.Xaml.Controls.Image()
                                    {
                                        Source = new BitmapImage(srcUri)
                                    }
                                };

                                parent.AddToParentParagraph(img);
                            }
                            break;
                    }
                }
            }
        }
        private static void ParseElement(XmlElement element, ITextContainer parent)
        {
            foreach (var child in element.ChildNodes)
            {
                if (child is Windows.Data.Xml.Dom.XmlText)
                {
                    if (string.IsNullOrEmpty(child.InnerText) ||
                        child.InnerText == "\n")
                    {
                        continue;
                    }

                    parent.Add(new Run { Text = child.InnerText.Trim(new char[] {'\n'}) });
                }
                else if (child is XmlElement)
                {
                    XmlElement e = (XmlElement)child;
                    switch (e.TagName.ToUpper())
                    {
                        case "P":
                            var paragraph = new Paragraph();
                            parent.Add(paragraph);
                            ParseElement(e, new ParagraphTextContainer(paragraph));
                            break;
                        case "STRONG":
                            var bold = new Bold();
                            parent.Add(bold);
                            ParseElement(e, new SpanTextContainer(bold));
                            break;
                        case "U":
                            var underline = new Underline();
                            parent.Add(underline);
                            ParseElement(e, new SpanTextContainer(underline));
                            break;
                        case "A":
                            var inlineElt = new InlineUIContainer();
                            var hyperlink = new HyperlinkButton();
                            inlineElt.Child = hyperlink;
                            hyperlink.Style = (Style)App.Current.Resources["HyperlinkButtonStyle"];
                            hyperlink.Content = e.InnerText;
                            hyperlink.Click += delegate(object sender, RoutedEventArgs eventArgs)
                            {
                                string uriString = e.GetAttribute("href");
                                Messenger.Default.Send<LinkClickedMessage>(new LinkClickedMessage()
                                {
                                    UriToNavigate = uriString
                                });
                            };
                            parent.Add(inlineElt);
                            //ParseElement(e, parent);
                            break;
                        case "BR":
                                parent.Add(new LineBreak());
                            break;
                    }
                }

            }
        }
        /// <summary>
        /// Renders a header element.
        /// </summary>
        /// <param name="element"></param>
        /// <param name="blockUIElementCollection"></param>
        /// <param name="context"></param>
        private void RenderHeader(HeaderBlock element, UIElementCollection blockUIElementCollection, RenderContext context)
        {
            var textBlock = CreateOrReuseRichTextBlock(blockUIElementCollection, context);

            var paragraph = new Paragraph();
            var childInlines = paragraph.Inlines;
            switch (element.HeaderLevel)
            {
                case 1:
                    paragraph.Margin = Header1Margin;
                    paragraph.FontSize = Header1FontSize;
                    paragraph.FontWeight = Header1FontWeight;
                    break;
                case 2:
                    paragraph.Margin = Header2Margin;
                    paragraph.FontSize = Header2FontSize;
                    paragraph.FontWeight = Header2FontWeight;
                    break;
                case 3:
                    paragraph.Margin = Header3Margin;
                    paragraph.FontSize = Header3FontSize;
                    paragraph.FontWeight = Header3FontWeight;
                    break;
                case 4:
                    paragraph.Margin = Header4Margin;
                    paragraph.FontSize = Header4FontSize;
                    paragraph.FontWeight = Header4FontWeight;
                    break;
                case 5:
                    paragraph.Margin = Header5Margin;
                    paragraph.FontSize = Header5FontSize;
                    paragraph.FontWeight = Header5FontWeight;
                    break;
                case 6:
                    paragraph.Margin = Header6Margin;
                    paragraph.FontSize = Header6FontSize;
                    paragraph.FontWeight = Header6FontWeight;

                    var underline = new Underline();
                    childInlines = underline.Inlines;
                    paragraph.Inlines.Add(underline);
                    break;
            }

            // Render the children into the para inline.
            context.TrimLeadingWhitespace = true;
            RenderInlineChildren(childInlines, element.Inlines, paragraph, context);

            // Add it to the blocks
            textBlock.Blocks.Add(paragraph);
        }
示例#14
0
        private static void ParseElement(XmlElement element, ITextContainer parent)
        {
            foreach (var child in element.ChildNodes)
            {
                if (child is Windows.Data.Xml.Dom.XmlText)
                {
                    if (string.IsNullOrEmpty(child.InnerText) ||
                        child.InnerText == "\n")
                    {
                        continue;
                    }

                    parent.Add(new Run { Text = ((XmlText)child).InnerText });
                }
                else if (child is XmlElement)
                {
                    XmlElement e = (XmlElement)child;
                    switch (e.TagName.ToUpper())
                    {
                        case "DIV":
                            if (e.GetAttribute("class") == "p")
                            {
                                Paragraph paragraph = new Paragraph();
                                paragraph.Margin = new Thickness(0, 0, 0, 10);
                                //paragraph.LineHeight = 20;
                                parent.Add(paragraph);
                                ParseElement(e, new ParagraphTextContainer(paragraph));

                            }
                            else if (e.GetAttribute("class") == "label" && element.GetAttribute("class").Contains("chapter"))
                            {
                                Paragraph chapterLabel = new Paragraph();
                                chapterLabel.FontFamily = new FontFamily("Segoe UI");
                                chapterLabel.FontSize = 32;
                                chapterLabel.Inlines.Add(new Run { Text = "Chapter " + e.InnerText });
                                parent.Add(chapterLabel);

                            }
                            else
                            {
                                ParseElement(e, parent);
                            }
                            break;
                        case "SPAN":
                            //ParseElement(e, parent);

                            if (e.GetAttribute("class") == "heading")
                            {
                                Paragraph header = new Paragraph();
                                header.FontFamily = new FontFamily("Segoe UI");
                                header.FontSize = 20;
                                header.Margin = new Thickness(0, 10, 0, 5);
                                header.Inlines.Add(new Run { Text = e.InnerText });
                                parent.Add(header);
                            }
                            else if (e.GetAttribute("class") == "label" && element.GetAttribute("class").Contains("verse"))
                            {
                                Run run = new Run();
                                run.Text = e.InnerText;
                                run.FontSize = 8;
                                parent.Add(run);
                            }
                            else
                            {
                                ParseElement(e, parent);
                            }
                            //else
                            //{
                            //    Run run = new Run();
                            //    run.Text = e.InnerText;
                            //    parent.Add(run);
                            //}
                            break;
                        case "P":
                            var p = new Paragraph();
                            parent.Add(p);
                            ParseElement(e, new ParagraphTextContainer(p));
                            break;
                        case "STRONG":
                            var bold = new Bold();
                            parent.Add(bold);
                            ParseElement(e, new SpanTextContainer(bold));
                            break;
                        case "U":
                            var underline = new Underline();
                            parent.Add(underline);
                            ParseElement(e, new SpanTextContainer(underline));
                            break;
                        case "A":
                            ParseElement(e, parent);
                            break;
                        case "BR":
                            parent.Add(new LineBreak());
                            break;
                    }
                }

            }
        }
 private static Inline GenerateUnderline(HtmlNode node)
 {
     Debug.WriteLine("Properties >>>>>> GenerateUnderline");
     Underline u = new Underline();
     AddChildren(u, node);
     return u;
 }
示例#16
0
        private static void AddUnderline(TextBlock textBlock, string content)
        {
            Run run = new Run();
            run.Text = content;
            run.Foreground = new SolidColorBrush(Colors.Blue);
            Underline underline = new Underline();
            underline.Inlines.Add(run);

            textBlock.Inlines.Add(underline);
        }