public void StylePropertiesTest()
        {
            var settings = new CTParagraphStyleSettings()
            {
                TailIndent             = 5,
                ParagraphSpacingBefore = 5,
                ParagraphSpacing       = 5,
                LineSpacing            = 5,
                MinimumLineHeight      = 5,
                MaximumLineHeight      = 5,
                LineHeightMultiple     = 5,
                DefaultTabInterval     = 5,
                HeadIndent             = 5,
                FirstLineHeadIndent    = 5,
                LineBreakMode          = CTLineBreakMode.TruncatingHead,
                BaseWritingDirection   = CTWritingDirection.Natural,
                Alignment = CTTextAlignment.Justified,
                TabStops  = new [] {
                    new CTTextTab(CTTextAlignment.Justified, 2),
                    new CTTextTab(CTTextAlignment.Natural, 1)
                }
            };

            var style = new CTParagraphStyle(settings);

            Assert.DoesNotThrow(() => {
                Assert.AreEqual(settings.TailIndent, (nfloat)style.TailIndent, "TailIndent");
                Assert.AreEqual(settings.ParagraphSpacingBefore, (nfloat)style.ParagraphSpacingBefore, "ParagraphSpacingBefore");
                Assert.AreEqual(settings.ParagraphSpacing, (nfloat)style.ParagraphSpacing, "ParagraphSpacing");
                Assert.AreEqual(settings.LineSpacing, (nfloat)style.LineSpacing, "LineSpacing");
                Assert.AreEqual(settings.MinimumLineHeight, (nfloat)style.MinimumLineHeight, "MinimumLineHeight");
                Assert.AreEqual(settings.MaximumLineHeight, (nfloat)style.MaximumLineHeight, "MaximumLineHeight");
                Assert.AreEqual(settings.LineHeightMultiple, (nfloat)style.LineHeightMultiple, "LineHeightMultiple");
                Assert.AreEqual(settings.DefaultTabInterval, (nfloat)style.DefaultTabInterval, "DefaultTabInterval");
                Assert.AreEqual(settings.HeadIndent, (nfloat)style.HeadIndent, "HeadIndent");
                Assert.AreEqual(settings.FirstLineHeadIndent, (nfloat)style.FirstLineHeadIndent, "FirstLineHeadIndent");
                Assert.AreEqual(settings.LineBreakMode, style.LineBreakMode, "LineBreakMode");
                Assert.AreEqual(settings.BaseWritingDirection, style.BaseWritingDirection, "LineBreakMode");
                Assert.AreEqual(settings.Alignment, style.Alignment, "Alignment");

                var styleTabStops = style.GetTabStops();
                Assert.AreEqual(settings.TabStops.Count(), styleTabStops.Length, "TabStops");
                Assert.True(styleTabStops.Any(t => t.Location == 2 && t.TextAlignment == CTTextAlignment.Justified));
                Assert.True(styleTabStops.Any(t => t.Location == 1 && t.TextAlignment == CTTextAlignment.Natural));
            });
        }
        private static NSMutableAttributedString buildAttributedString(string text, CTFont font,
                                                                       Color?fontColor = null)
        {
            // Create a new attributed string from text
            NSMutableAttributedString atts =
                new NSMutableAttributedString(text);

            var attRange = new NSRange(0, atts.Length);
            var attsDic  = new NSMutableDictionary();

            // Font attribute
            NSObject fontObject = new NSObject(font.Handle);

            attsDic.Add(FontAttributedName, fontObject);
            // -- end font

            if (fontColor.HasValue)
            {
                // Font color
                var     fc    = fontColor.Value;
                NSColor color = NSColor.FromDeviceRgba(fc.R / 255f,
                                                       fc.G / 255f,
                                                       fc.B / 255f,
                                                       fc.A / 255f);
                NSObject colorObject = new NSObject(color.Handle);
                attsDic.Add(ForegroundColorAttributedName, colorObject);
                // -- end font Color
            }

            if (underLine)
            {
                // Underline
                int single          = (int)MonoMac.AppKit.NSUnderlineStyle.Single;
                int solid           = (int)MonoMac.AppKit.NSUnderlinePattern.Solid;
                var attss           = single | solid;
                var underlineObject = NSNumber.FromInt32(attss);
                //var under = NSAttributedString.UnderlineStyleAttributeName.ToString();
                attsDic.Add(UnderlineStyleAttributeName, underlineObject);
                // --- end underline
            }


            if (strikeThrough)
            {
                // StrikeThrough
                //				NSColor bcolor = NSColor.Blue;
                //				NSObject bcolorObject = new NSObject(bcolor.Handle);
                //				attsDic.Add(NSAttributedString.StrikethroughColorAttributeName, bcolorObject);
                int stsingle          = (int)MonoMac.AppKit.NSUnderlineStyle.Single;
                int stsolid           = (int)MonoMac.AppKit.NSUnderlinePattern.Solid;
                var stattss           = stsingle | stsolid;
                var stunderlineObject = NSNumber.FromInt32(stattss);

                attsDic.Add(StrikethroughStyleAttributeName, stunderlineObject);
                // --- end underline
            }


            // Text alignment
            var alignment         = CTTextAlignment.Left;
            var alignmentSettings = new CTParagraphStyleSettings();

            alignmentSettings.Alignment = alignment;
            var      paragraphStyle = new CTParagraphStyle(alignmentSettings);
            NSObject psObject       = new NSObject(paragraphStyle.Handle);

            // end text alignment

            attsDic.Add(ParagraphStyleAttributeName, psObject);

            atts.SetAttributes(attsDic, attRange);

            return(atts);
        }
        private static NSMutableAttributedString buildAttributedString(string text, Font font,
                                                                       Color?fontColor = null)
        {
            // Create a new attributed string definition
            var ctAttributes = new CTStringAttributes();

            // Font attribute
            ctAttributes.Font = font.nativeFont;
            // -- end font

            if (fontColor.HasValue)
            {
                // Font color
                var fc      = fontColor.Value;
                var cgColor = new CGColor(fc.R / 255f,
                                          fc.G / 255f,
                                          fc.B / 255f,
                                          fc.A / 255f);

                ctAttributes.ForegroundColor            = cgColor;
                ctAttributes.ForegroundColorFromContext = false;
                // -- end font Color
            }

            if (font.Underline)
            {
                // Underline
#if MONOMAC
                int single = (int)AppKit.NSUnderlineStyle.Single;
                int solid  = (int)AppKit.NSUnderlinePattern.Solid;
                var attss  = single | solid;
                ctAttributes.UnderlineStyleValue = attss;
#else
                ctAttributes.UnderlineStyleValue = 1;
#endif
                // --- end underline
            }


            if (font.Strikeout)
            {
                // StrikeThrough
                //				NSColor bcolor = NSColor.Blue;
                //				NSObject bcolorObject = new NSObject(bcolor.Handle);
                //				attsDic.Add(NSAttributedString.StrikethroughColorAttributeName, bcolorObject);
                //				#if MACOS
                //				int stsingle = (int)MonoMac.AppKit.NSUnderlineStyle.Single;
                //				int stsolid = (int)MonoMac.AppKit.NSUnderlinePattern.Solid;
                //				var stattss = stsingle | stsolid;
                //				var stunderlineObject = NSNumber.FromInt32(stattss);
                //				#else
                //				var stunderlineObject = NSNumber.FromInt32 (1);
                //				#endif
                //
                //				attsDic.Add(StrikethroughStyleAttributeName, stunderlineObject);
                // --- end underline
            }


            // Text alignment
            var alignment         = CTTextAlignment.Left;
            var alignmentSettings = new CTParagraphStyleSettings();
            alignmentSettings.Alignment = alignment;
            var paragraphStyle = new CTParagraphStyle(alignmentSettings);

            ctAttributes.ParagraphStyle = paragraphStyle;
            // end text alignment

            NSMutableAttributedString atts =
                new NSMutableAttributedString(text, ctAttributes.Dictionary);

            return(atts);
        }
Example #4
0
        public SizeF GetStringSize(string aString, string aFontName, float aFontSize, HorizontalAlignment aHorizontalAlignment, VerticalAlignment aVerticalAlignment)
        {
            var   fontSize = aFontSize;
            float factor   = 1;

            while (fontSize > 10)
            {
                fontSize /= 10;
                factor   *= 10;
            }

            var vPath = new CGPath();

            vPath.AddRect(new CGRect(0, 0, 512, 512));
            vPath.CloseSubpath();

            var vAttributedString = new NSMutableAttributedString(aString);

            var vAttributes = new CTStringAttributes();

            // Load the font
            var vFont = NativeFontService.Instance.LoadFont(aFontName ?? _systemFontName, (float)fontSize);

            vAttributes.Font = vFont;

            // Set the horizontal alignment
            var vParagraphSettings = new CTParagraphStyleSettings();

            switch (aHorizontalAlignment)
            {
            case HorizontalAlignment.Left:
                vParagraphSettings.Alignment = CTTextAlignment.Left;
                break;

            case HorizontalAlignment.Center:
                vParagraphSettings.Alignment = CTTextAlignment.Center;
                break;

            case HorizontalAlignment.Right:
                vParagraphSettings.Alignment = CTTextAlignment.Right;
                break;

            case HorizontalAlignment.Justified:
                vParagraphSettings.Alignment = CTTextAlignment.Justified;
                break;
            }

            var vParagraphStyle = new CTParagraphStyle(vParagraphSettings);

            vAttributes.ParagraphStyle = vParagraphStyle;

            // Set the attributes for the complete length of the string
            vAttributedString.SetAttributes(vAttributes, new NSRange(0, aString.Length));

            // Create the framesetter with the attributed string.
            var vFrameSetter = new CTFramesetter(vAttributedString);

            var textBounds = GetTextSize(vFrameSetter, vPath);

            //Logger.Debug("{0} {1}",vSize,aString);

            vFrameSetter.Dispose();
            vAttributedString.Dispose();
            vParagraphStyle.Dispose();
            //vFont.Dispose();
            vPath.Dispose();

            textBounds.Width  *= factor;
            textBounds.Height *= factor;

            //vSize.Width = Math.Ceiling(vSize.Width);
            //vSize.Height = Math.Ceiling(vSize.Height);

            return(textBounds.Size);
        }
Example #5
0
        private NSMutableAttributedString buildAttributedString(string text, Font font, StringFormat format = null,
                                                                Color?fontColor = null)
        {
            // Create a new attributed string definition
            var ctAttributes = new CTStringAttributes();

            // Font attribute
            ctAttributes.Font = font.nativeFont;
            // -- end font

            if (format != null && (format.FormatFlags & StringFormatFlags.DirectionVertical) == StringFormatFlags.DirectionVertical)
            {
                //ctAttributes.VerticalForms = true;
            }

            if (fontColor.HasValue)
            {
                // Font color
                var fc      = fontColor.Value;
                var cgColor = new CGColor(fc.R / 255f,
                                          fc.G / 255f,
                                          fc.B / 255f,
                                          fc.A / 255f);

                ctAttributes.ForegroundColor            = cgColor;
                ctAttributes.ForegroundColorFromContext = false;
                // -- end font Color
            }

            if (font.Underline)
            {
                // Underline
#if MONOMAC
                int single = (int)AppKit.NSUnderlineStyle.Single;
                int solid  = (int)AppKit.NSUnderlinePattern.Solid;
                var attss  = single | solid;
                ctAttributes.UnderlineStyleValue = attss;
#else
                ctAttributes.UnderlineStyleValue = 1;
#endif
                // --- end underline
            }


            if (font.Strikeout)
            {
                // StrikeThrough
#if MONOMAC
                int single = (int)AppKit.NSUnderlineStyle.Single;
                int solid  = (int)AppKit.NSUnderlinePattern.Solid;
                var attss  = single | solid;
                ctAttributes.UnderlineStyleValue = attss;
#else
                ctAttributes.UnderlineStyleValue = 1;
#endif

                // --- end StrikeThrough
            }


            var alignment         = CTTextAlignment.Left;
            var alignmentSettings = new CTParagraphStyleSettings();
            alignmentSettings.Alignment = alignment;
            var paragraphStyle = new CTParagraphStyle(alignmentSettings);

            ctAttributes.ParagraphStyle = paragraphStyle;
            // end text alignment

            NSMutableAttributedString atts =
                new NSMutableAttributedString(text, ctAttributes.Dictionary);

            return(atts);
        }
        private NSMutableAttributedString buildAttributedString(string text, Font font, StringFormat format = null, 
			Color? fontColor=null)
        {
            // Create a new attributed string definition
            var ctAttributes = new CTStringAttributes ();

            // Font attribute
            ctAttributes.Font = font.nativeFont;
            // -- end font

            if (format != null && (format.FormatFlags & StringFormatFlags.DirectionVertical) == StringFormatFlags.DirectionVertical)
            {
                //ctAttributes.VerticalForms = true;

            }

            if (fontColor.HasValue) {

                // Font color
                var fc = fontColor.Value;
                var cgColor = new CGColor(fc.R / 255f,
                    fc.G / 255f,
                    fc.B / 255f,
                    fc.A / 255f);

                ctAttributes.ForegroundColor = cgColor;
                ctAttributes.ForegroundColorFromContext = false;
                // -- end font Color
            }

            if (font.Underline) {
                // Underline
            #if MONOMAC
                int single = (int)AppKit.NSUnderlineStyle.Single;
                int solid = (int)AppKit.NSUnderlinePattern.Solid;
                var attss = single | solid;
                ctAttributes.UnderlineStyleValue = attss;
            #else
                ctAttributes.UnderlineStyleValue = 1;
            #endif
                // --- end underline
            }

            if (font.Strikeout) {
                // StrikeThrough
            #if MONOMAC
                int single = (int)AppKit.NSUnderlineStyle.Single;
                int solid = (int)AppKit.NSUnderlinePattern.Solid;
                var attss = single | solid;
                ctAttributes.UnderlineStyleValue = attss;
            #else
                ctAttributes.UnderlineStyleValue = 1;
            #endif

                // --- end StrikeThrough
            }

            var alignment = CTTextAlignment.Left;
            var alignmentSettings = new CTParagraphStyleSettings();
            alignmentSettings.Alignment = alignment;
            var paragraphStyle = new CTParagraphStyle(alignmentSettings);

            ctAttributes.ParagraphStyle = paragraphStyle;
            // end text alignment

            NSMutableAttributedString atts =
                new NSMutableAttributedString(text,ctAttributes.Dictionary);

            return atts;
        }
        private static NSMutableAttributedString buildAttributedString(string text, Font font, 
		                                                        Color? fontColor=null)
        {
            // Create a new attributed string definition
            var ctAttributes = new CTStringAttributes ();

            // Font attribute
            ctAttributes.Font = font.nativeFont;
            // -- end font

            if (fontColor.HasValue) {

                // Font color
                var fc = fontColor.Value;
                var cgColor = new CGColor(fc.R / 255f,
                                          fc.G / 255f,
                                          fc.B / 255f,
                                          fc.A / 255f);

                ctAttributes.ForegroundColor = cgColor;
                ctAttributes.ForegroundColorFromContext = false;
                // -- end font Color
            }

            if (font.Underline) {
                // Underline
            #if MONOMAC
                int single = (int)AppKit.NSUnderlineStyle.Single;
                int solid = (int)AppKit.NSUnderlinePattern.Solid;
                var attss = single | solid;
                ctAttributes.UnderlineStyleValue = attss;
            #else
                ctAttributes.UnderlineStyleValue = 1;
            #endif
                // --- end underline
            }

            if (font.Strikeout) {
                // StrikeThrough
                //				NSColor bcolor = NSColor.Blue;
                //				NSObject bcolorObject = new NSObject(bcolor.Handle);
                //				attsDic.Add(NSAttributedString.StrikethroughColorAttributeName, bcolorObject);
                //				#if MACOS
                //				int stsingle = (int)MonoMac.AppKit.NSUnderlineStyle.Single;
                //				int stsolid = (int)MonoMac.AppKit.NSUnderlinePattern.Solid;
                //				var stattss = stsingle | stsolid;
                //				var stunderlineObject = NSNumber.FromInt32(stattss);
                //				#else
                //				var stunderlineObject = NSNumber.FromInt32 (1);
                //				#endif
                //
                //				attsDic.Add(StrikethroughStyleAttributeName, stunderlineObject);
                // --- end underline
            }

            // Text alignment
            var alignment = CTTextAlignment.Left;
            var alignmentSettings = new CTParagraphStyleSettings();
            alignmentSettings.Alignment = alignment;
            var paragraphStyle = new CTParagraphStyle(alignmentSettings);

            ctAttributes.ParagraphStyle = paragraphStyle;
            // end text alignment

            NSMutableAttributedString atts =
                new NSMutableAttributedString(text,ctAttributes.Dictionary);

            return atts;
        }
        public SizeF GetStringSize(
            string value,
            string fontName,
            float fontSize,
            HorizontalAlignment horizontalAlignment,
            VerticalAlignment verticalAlignment)
        {
            float factor = 1;

            while (fontSize > 10)
            {
                fontSize /= 10;
                factor   *= 10;
            }

            var path = new CGPath();

            path.AddRect(new Drawing.RectangleF(0, 0, 512, 512));
            path.CloseSubpath();

            var attributedString = new NSMutableAttributedString(value);

            var attributes = new CTStringAttributes();

            // Load the font
            var font = NativeFontService.Instance.LoadFont(fontName ?? SystemFontName, fontSize);

            attributes.Font = font;

            // Set the horizontal alignment
            var paragraphSettings = new CTParagraphStyleSettings();

            switch (horizontalAlignment)
            {
            case HorizontalAlignment.Left:
                paragraphSettings.Alignment = CTTextAlignment.Left;
                break;

            case HorizontalAlignment.Center:
                paragraphSettings.Alignment = CTTextAlignment.Center;
                break;

            case HorizontalAlignment.Right:
                paragraphSettings.Alignment = CTTextAlignment.Right;
                break;

            case HorizontalAlignment.Justified:
                paragraphSettings.Alignment = CTTextAlignment.Justified;
                break;
            }

            var paragraphStyle = new CTParagraphStyle(paragraphSettings);

            attributes.ParagraphStyle = paragraphStyle;

            // Set the attributes for the complete length of the string
            attributedString.SetAttributes(attributes, new NSRange(0, value.Length));

            // Create the frame setter with the attributed string.
            var frameSetter = new CTFramesetter(attributedString);

            var textBounds = GetTextSize(frameSetter, path);

            //Logger.Debug("{0} {1}",vSize,aString);

            frameSetter.Dispose();
            attributedString.Dispose();
            paragraphStyle.Dispose();
            //font.Dispose();
            path.Dispose();

            textBounds.Width  *= factor;
            textBounds.Height *= factor;

            //size.Width = Math.Ceiling(vSize.Width);
            //size.Height = Math.Ceiling(vSize.Height);

            return(textBounds.Size);
        }