I was experimenting with a new method of inserting the text and managed to get this which works really well altho it doesn't fix my issue with inserting urls that are in 2 different Runs but that is a minor issue.
Code:private void InsertHyperlink(TextPointer position) { string match = string.Empty; Regex r = new Regex("(?:^|[\\s\\[\\]\\}\\{\\(\\)\\\'\\\"<>])((?:(?:https?|gopher|ftp|file|irc):\\/\\/|www\\.)[a-zA-Z0-9\\.\\-=;&%\\?]+(?:\\/?[a-zA-Z0-9\\.\\-=;&%\\?]*)*)"); Hyperlink h; while (position != null) { if (position.CompareTo(this.Document.ContentEnd) == 0) { break; } if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { String text = position.GetTextInRun(LogicalDirection.Forward); Int32 indexInRun = -1; if (r.IsMatch(text)) { Match m = r.Match(text); match = m.Groups[1].Value; indexInRun = m.Groups[1].Index; } if (indexInRun >= 0) { position = position.GetPositionAtOffset(indexInRun); h = new Hyperlink(position, position.GetPositionAtOffset(match.Length)); h.Tag = match; h.Foreground = Brushes.Blue; h.TextDecorations = TextDecorations.Underline; h.Cursor = System.Windows.Input.Cursors.Hand; h.MouseLeftButtonDown += new MouseButtonEventHandler(h_MouseLeftButtonDown); position = position.GetPositionAtOffset(match.Length); } else { position = position.GetPositionAtOffset(text.Length); } } else { position = position.GetNextContextPosition(LogicalDirection.Forward); } } }




Reply With Quote