private static NSMutableAttributedString RemoveTrailingNewLine(NSAttributedString htmlString)
        {
            NSAttributedString lastCharRange = htmlString.Substring(htmlString.Length - 1, 1);

            if (lastCharRange.Value == "\n")
            {
                htmlString = htmlString.Substring(0, htmlString.Length - 1);
            }

            return(new NSMutableAttributedString(htmlString));
        }
Exemple #2
0
        internal static NSMutableAttributedString RemoveTrailingNewLines(this NSAttributedString htmlString)
        {
            var count = 0;

            for (int i = 1; i <= htmlString.Length; i++)
            {
                if ("\n" != htmlString.Substring(htmlString.Length - i, 1).Value)
                {
                    break;
                }

                count++;
            }

            if (count > 0)
            {
                htmlString = htmlString.Substring(0, htmlString.Length - count);
            }

            return(new NSMutableAttributedString(htmlString));
        }
        public static IEnumerable <ISpan> AsSpans(this NSAttributedString text, int cursorPosition)
        {
            var  start = 0;
            bool queryTextSpanAlreadyUsed = false;

            while (start != text.Length)
            {
                var attributes = text.GetAttributes(start, out var longestEffectiveRange, new NSRange(start, text.Length - start));
                var length     = (int)longestEffectiveRange.Length;
                var end        = start + length;

                if (attributes.ContainsKey(ProjectId))
                {
                    var(projectId, projectName, projectColor, taskId, taskName) = attributes.GetProjectInformation();

                    yield return(new ProjectSpan(projectId, projectName, projectColor, taskId, taskName));
                }
                else if (attributes.ContainsKey(TagId))
                {
                    var(tagId, tagName) = attributes.GetTagInformation();
                    yield return(new TagSpan(tagId, tagName));
                }
                else if (length > 0)
                {
                    var subText = text.Substring(start, length).ToString().Substring(0, length);
                    if (queryTextSpanAlreadyUsed == false && cursorPosition.IsInRange(start, end))
                    {
                        queryTextSpanAlreadyUsed = true;
                        yield return(new QueryTextSpan(subText, cursorPosition - start));
                    }
                    else
                    {
                        yield return(new TextSpan(subText));
                    }
                }

                start = end;
            }
        }
Exemple #4
0
        private static bool HandleAttributes(
            IList <IAttributedTextRun> runs,
            TextWriter writer,
            NSAttributedString target,
            NSDictionary attrs,
            NSRange range)
        {
            var text = target.Substring(range.Location, range.Length).Value;

            var formatAttributes = new TextAttributes();
            var run = new AttributedTextRun((int)range.Location, (int)range.Length, formatAttributes);

            NSObject font;

            if (attrs.TryGetValue(NSStringAttributeKey.Font, out font))
            {
                var actualFont = (NSFont)font;
#if __MACOS__
                var fontName = actualFont.FontName;
#else
                var fontName = actualFont.Name;
#endif

                formatAttributes.SetFontSize((float)actualFont.PointSize);
                if (!fontName.StartsWith(".", System.StringComparison.Ordinal))
                {
                    formatAttributes.SetFontName(fontName);
                }
                else
                {
                    if (fontName.Contains("Italic"))
                    {
                        formatAttributes.SetItalic(true);
                    }

                    if (fontName.Contains("Bold"))
                    {
                        formatAttributes.SetBold(true);
                    }
                }
            }

            NSObject underline;
            if (attrs.TryGetValue(NSStringAttributeKey.UnderlineStyle, out underline))
            {
                var number = underline as NSNumber;
                if (number != null && number.Int32Value > 0)
                {
                    formatAttributes.SetUnderline(true);
                }
            }

            NSObject strikethrough;
            if (attrs.TryGetValue(NSStringAttributeKey.StrikethroughStyle, out strikethrough))
            {
                var number = strikethrough as NSNumber;
                if (number != null && number.Int32Value > 0)
                {
                    formatAttributes.SetStrikethrough(true);
                }
            }

#if MONOMAC
            NSObject superscript;
            if (attrs.TryGetValue(NSStringAttributeKey.Superscript, out superscript))
            {
                var number = superscript as NSNumber;
                if (number != null && number.Int32Value == -1)
                {
                    formatAttributes.SetSubscript(true);
                }
                else if (number != null && number.Int32Value == 1)
                {
                    formatAttributes.SetSuperscript(true);
                }
            }
#endif

            NSObject color;
            if (attrs.TryGetValue(NSStringAttributeKey.ForegroundColor, out color))
            {
                var colorObject = color as NSColor;
                if (colorObject != null)
                {
                    formatAttributes.SetForegroundColor(colorObject.ToHex());
                }
            }

            NSObject backgroundColor;
            if (attrs.TryGetValue(NSStringAttributeKey.BackgroundColor, out backgroundColor))
            {
                var colorObject = backgroundColor as NSColor;
                if (colorObject != null)
                {
                    formatAttributes.SetBackgroundColor(colorObject.ToHex());
                }
            }

#if MONOMAC
            NSObject paragraphStyleAsObject;
            if (attrs.TryGetValue(NSStringAttributeKey.ParagraphStyle, out paragraphStyleAsObject))
            {
                var paragraphStyle = (NSParagraphStyle)paragraphStyleAsObject;
                if (paragraphStyle.TextLists != null && paragraphStyle.TextLists.Length > 0)
                {
                    for (int i = 0; i < paragraphStyle.TextLists.Length; i++)
                    {
                        var textList     = paragraphStyle.TextLists [i];
                        var markerFormat = textList.MarkerFormat;

                        if ("{hyphen}".Equals(textList.MarkerFormat))
                        {
                            formatAttributes.SetUnorderedList(true);
                            formatAttributes.SetMarker(MarkerType.Hyphen);
                        }
                        else
                        {
                            formatAttributes.SetUnorderedList(true);
                            formatAttributes.SetMarker(MarkerType.ClosedCircle);
                        }
                    }
                }
            }
#endif

            if (run.Attributes.Count > 0)
            {
                runs.Add(run);
            }

            writer.Write(text);
            return(false);
        }