Esempio n. 1
0
        public static NSMutableAttributedString?WithLineHeight(this NSAttributedString attributedString, double lineHeight)
        {
            if (attributedString == null || attributedString.Length == 0)
            {
                return(null);
            }

            var attribute = (NSParagraphStyle)attributedString.GetAttribute(UIStringAttributeKey.ParagraphStyle, 0, out _);

            // if we need to un-set the line height but there is no attribute to modify then we do nothing
            if (lineHeight == -1 && attribute == null)
            {
                return(null);
            }

            var mutableParagraphStyle = new NSMutableParagraphStyle();

            if (attribute != null)
            {
                mutableParagraphStyle.SetParagraphStyle(attribute);
            }

            mutableParagraphStyle.LineHeightMultiple = new nfloat(lineHeight >= 0 ? lineHeight : -1);

            var mutableAttributedString = new NSMutableAttributedString(attributedString);

            mutableAttributedString.AddAttribute
            (
                UIStringAttributeKey.ParagraphStyle,
                mutableParagraphStyle,
                new NSRange(0, mutableAttributedString.Length)
            );

            return(mutableAttributedString);
        }
Esempio n. 2
0
        public void Draw(NSRect aCellFrame, NSView aControlView)
        {
            BookmarkData item = ObjectValue.CastAs <BookmarkData>();

            // create attributes for the title and subtitle text
            NSMutableParagraphStyle style = new NSMutableParagraphStyle();

            style.SetParagraphStyle(NSParagraphStyle.DefaultParagraphStyle);
            style.SetLineBreakMode(NSLineBreakMode.NSLineBreakByTruncatingTail);

            NSDictionary titleAttr = NSDictionary.DictionaryWithObjectsAndKeys(FontManager.FontMedium, NSAttributedString.NSFontAttributeName,
                                                                               NSColor.WhiteColor, NSAttributedString.NSForegroundColorAttributeName,
                                                                               style, NSAttributedString.NSParagraphStyleAttributeName,
                                                                               null);
            NSDictionary subtitleAttr = NSDictionary.DictionaryWithObjectsAndKeys(FontManager.FontSmall, NSAttributedString.NSFontAttributeName,
                                                                                  NSColor.WhiteColor, NSAttributedString.NSForegroundColorAttributeName,
                                                                                  style, NSAttributedString.NSParagraphStyleAttributeName,
                                                                                  null);

            style.Release();

            NSPoint pt = aCellFrame.origin;

            // draw title
            NSString title = NSString.StringWithUTF8String(item.Bookmark.Title);

            title.DrawAtPointWithAttributes(pt, titleAttr);

            NSSize size = title.SizeWithAttributes(titleAttr);

            pt.y += size.height;

            // draw breadcrumb
            List <string> titleTrail = new List <string>();

            foreach (Breadcrumb bc in item.Bookmark.BreadcrumbTrail)
            {
                titleTrail.Add(bc.Title);
            }
            string   breadcrumb  = string.Join("/", titleTrail.ToArray());
            NSString breadcrumb2 = NSString.StringWithUTF8String(breadcrumb);

            breadcrumb2.DrawAtPointWithAttributes(pt, subtitleAttr);
        }
Esempio n. 3
0
        public override void DrawRect(NSRect aRect)
        {
            // draw bars
            if (iMaxValue > 0)
            {
                // draw the current value bar
                NSColor colour = NSColor.ColorWithCalibratedRedGreenBlueAlpha(71.0f / 255.0f, 172.0f / 255.0f, 220.0f / 255.0f, 1.0f);
                colour.SetStroke();

                DrawArc(0, iValue, true);

                // draw the preview bar
                if (iPreviewEnabled)
                {
                    colour = NSColor.ColorWithCalibratedRedGreenBlueAlpha(187.0f / 255.0f, 187.0f / 255.0f, 0.0f / 255.0f, 1.0f);
                    colour.SetStroke();

                    DrawArc(iValue, iPreviewValue, (iPreviewValue > iValue));
                }
            }

            // draw text
            if (iText != null)
            {
                NSMutableParagraphStyle style = new NSMutableParagraphStyle();
                style.SetParagraphStyle(NSParagraphStyle.DefaultParagraphStyle);
                style.SetAlignment(NSTextAlignment.NSCenterTextAlignment);

                NSDictionary dict = NSDictionary.DictionaryWithObjectsAndKeys(FontManager.FontMedium, NSAttributedString.NSFontAttributeName,
                                                                              style, NSAttributedString.NSParagraphStyleAttributeName,
                                                                              NSColor.WhiteColor, NSAttributedString.NSForegroundColorAttributeName,
                                                                              null);
                style.Release();

                NSSize size = iText.SizeWithAttributes(dict);
                NSRect rect = new NSRect((Bounds.Width - size.width) * 0.5f, (Bounds.Height - size.height) * 0.5f, size.width, size.height);

                iText.DrawInRectWithAttributes(rect, dict);
            }
        }
Esempio n. 4
0
        internal CCTexture2D CreateTextSprite(string text, CCFontDefinition textDefinition)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(new CCTexture2D());
            }

            int imageWidth;
            int imageHeight;
            var textDef = textDefinition;
            var contentScaleFactorWidth  = CCLabel.DefaultTexelToContentSizeRatios.Width;
            var contentScaleFactorHeight = CCLabel.DefaultTexelToContentSizeRatios.Height;

            textDef.FontSize          *= contentScaleFactorWidth;
            textDef.Dimensions.Width  *= contentScaleFactorWidth;
            textDef.Dimensions.Height *= contentScaleFactorHeight;

            //bool hasPremultipliedAlpha;

            // font
            NSFont font = null;

            var ext = System.IO.Path.GetExtension(textDef.FontName);

            if (!String.IsNullOrEmpty(ext) && ext.ToLower() == ".ttf")
            {
                try
                {
                    textDef.FontName = LoadFontFile(textDef.FontName);
                    font             = NSFont.FromFontName(textDef.FontName, textDef.FontSize);
                }
                catch
                {
                    CCLog.Log(".ttf {0} file not found or can not be loaded.", textDef.FontName);
                }
            }
            else
            {
                // font
                font = NSFontManager.SharedFontManager.FontWithFamily(textDef.FontName, NSFontTraitMask.Unbold | NSFontTraitMask.Unitalic, 0, textDef.FontSize);
            }

            if (font == null)
            {
                font = NSFontManager.SharedFontManager.FontWithFamily("Arial", NSFontTraitMask.Unbold | NSFontTraitMask.Unitalic, 0, textDef.FontSize);
                CCLog.Log("{0} not found.  Defaulting to Arial.", textDef.FontName);
            }

            // color
            var foregroundColor = NSColor.White;

            // alignment
            var horizontalAlignment = textDef.Alignment;
            var verticleAlignement  = textDef.LineAlignment;

            var textAlign = (CCTextAlignment.Right == horizontalAlignment) ? NSTextAlignment.Right
                : (CCTextAlignment.Center == horizontalAlignment) ? NSTextAlignment.Center
                : NSTextAlignment.Left;

            // LineBreak
            var lineBreak = (CCLabelLineBreak.Character == textDef.LineBreak) ? NSLineBreakMode.CharWrapping
                : (CCLabelLineBreak.Word == textDef.LineBreak) ? NSLineBreakMode.ByWordWrapping
                : NSLineBreakMode.Clipping;

            var nsparagraphStyle = new NSMutableParagraphStyle();

            nsparagraphStyle.SetParagraphStyle(NSMutableParagraphStyle.DefaultParagraphStyle);
            nsparagraphStyle.LineBreakMode = lineBreak;
            nsparagraphStyle.Alignment     = textAlign;

            // Create a new attributed string definition
            var nsAttributes = new NSStringAttributes();

            // Font attribute
            nsAttributes.Font            = font;
            nsAttributes.ForegroundColor = foregroundColor;
            nsAttributes.ParagraphStyle  = nsparagraphStyle;

            var stringWithAttributes = new NSAttributedString(text, nsAttributes);

            var realDimensions = stringWithAttributes.Size;

            // Mac crashes if the width or height is 0
            if (realDimensions == SizeF.Empty)
            {
                CCLog.Log("Native string:", "Dimensions of native NSAttributedString can not be 0,0");
                return(new CCTexture2D());
            }

            var dimensions = new SizeF(textDef.Dimensions.Width, textDef.Dimensions.Height);

            var layoutAvailable = true;

            //
            // * Note * This seems to only effect Mac because iOS works fine without this work around.
            // Right Alignment BoundingRectWithSize does not seem to be working correctly when the following conditions are set:
            //      1) Alignment Right
            //      2) No dimensions
            //      3) There are new line characters embedded in the string.
            //
            // So we set alignment to Left, calculate our bounds and then restore alignement afterwards before drawing.
            //
            if (dimensions.Width <= 0)
            {
                dimensions.Width = 8388608;
                layoutAvailable  = false;

                // Set our alignment variables to left - see notes above.
                nsparagraphStyle.Alignment = NSTextAlignment.Left;
                stringWithAttributes.Dispose();
                stringWithAttributes = null;
                stringWithAttributes = new NSAttributedString(text, nsAttributes);
            }

            if (dimensions.Height <= 0)
            {
                dimensions.Height = 8388608;
                layoutAvailable   = false;
            }

            // Calculate our bounding rectangle
            var boundingRect = stringWithAttributes.BoundingRectWithSize(new SizeF((int)dimensions.Width, (int)dimensions.Height),
                                                                         NSStringDrawingOptions.UsesLineFragmentOrigin);

            if (!layoutAvailable)
            {
                if (dimensions.Width == 8388608)
                {
                    dimensions.Width = boundingRect.Width;

                    // Restore our alignment before drawing - see notes above.
                    nsparagraphStyle.Alignment = textAlign;
                    stringWithAttributes.Dispose();
                    stringWithAttributes = null;
                    stringWithAttributes = new NSAttributedString(text, nsAttributes);
                }
                if (dimensions.Height == 8388608)
                {
                    dimensions.Height = boundingRect.Height;
                }
            }

            imageWidth  = (int)dimensions.Width;
            imageHeight = (int)dimensions.Height;

            // Alignment
            var xOffset = 0.0f;

            switch (textAlign)
            {
            case NSTextAlignment.Left:
                xOffset = 0;
                break;

            case NSTextAlignment.Center:
                xOffset = (dimensions.Width - boundingRect.Width) / 2.0f;
                break;

            case NSTextAlignment.Right: xOffset = dimensions.Width - boundingRect.Width; break;

            default: break;
            }

            // Line alignment
            var yOffset = (CCVerticalTextAlignment.Top == verticleAlignement ||
                           boundingRect.Height >= dimensions.Height) ? (dimensions.Height - boundingRect.Height) // align to top
                : (CCVerticalTextAlignment.Bottom == verticleAlignement) ? 0                                     // align to bottom
                : (imageHeight - boundingRect.Height) / 2.0f;                                                    // align to center

            //Find the rect that the string will draw into inside the dimensions
            var drawRect = new RectangleF(xOffset
                                          , yOffset
                                          , boundingRect.Width
                                          , boundingRect.Height);


            NSImage image = null;

            try
            {
                //Set antialias or not
                NSGraphicsContext.CurrentContext.ShouldAntialias = textDef.isShouldAntialias;

                image = new NSImage(new SizeF(imageWidth, imageHeight));

                image.LockFocus();

                // set a default transform
                var transform = new NSAffineTransform();
                transform.Set();

                stringWithAttributes.DrawInRect(drawRect);

                image.UnlockFocus();

                // We will use Texture2D from stream here instead of CCTexture2D stream.
                var tex = Texture2D.FromStream(CCDrawManager.SharedDrawManager.XnaGraphicsDevice, image);

                // Debugging purposes
//            var path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
//            var fileName = Path.Combine(path, "Label3.png");
//            using (var stream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
//            {
//                tex.SaveAsPng(stream, imageWidth, imageHeight);
//            }

                // Create our texture of the label string.
                var texture = new CCTexture2D(tex);

                return(texture);
            }
            catch (Exception exc)
            {
                CCLog.Log("CCLabel: Error creating native label:{0}\n{1}", exc.Message, exc.StackTrace);
            }
            finally
            {
                // clean up the resources
                if (image != null)
                {
                    image.Dispose();
                    image = null;
                }
                if (stringWithAttributes != null)
                {
                    stringWithAttributes.Dispose();
                    stringWithAttributes = null;
                }
            }
            return(new CCTexture2D());
        }
Esempio n. 5
0
        public void Draw(NSRect aCellFrame, NSView aControlView)
        {
            WrappedDataTrackItem wrapped = ObjectValue.CastAs <WrappedDataTrackItem>();
            IDataTrackItem       item    = wrapped.Item;

            // create attributes for the title and subtitle text
            NSMutableParagraphStyle style = new NSMutableParagraphStyle();

            style.SetParagraphStyle(NSParagraphStyle.DefaultParagraphStyle);
            style.SetLineBreakMode(NSLineBreakMode.NSLineBreakByTruncatingTail);

            // set font sizes depending on whether this item is a group or track
            NSFont titleFont    = item.IsGroup ? FontManager.FontLarge : FontManager.FontSemiLarge;
            NSFont subtitleFont = item.IsGroup ? FontManager.FontMedium : FontManager.FontSmall;

            NSDictionary titleAttr = NSDictionary.DictionaryWithObjectsAndKeys(titleFont, NSAttributedString.NSFontAttributeName,
                                                                               NSColor.WhiteColor, NSAttributedString.NSForegroundColorAttributeName,
                                                                               style, NSAttributedString.NSParagraphStyleAttributeName,
                                                                               null);
            NSDictionary subtitleAttr = NSDictionary.DictionaryWithObjectsAndKeys(subtitleFont, NSAttributedString.NSFontAttributeName,
                                                                                  NSColor.WhiteColor, NSAttributedString.NSForegroundColorAttributeName,
                                                                                  style, NSAttributedString.NSParagraphStyleAttributeName,
                                                                                  null);

            style.Release();

            float  vertPos     = aCellFrame.origin.y;
            float  titleIndent = 0.0f;
            NSSize size;
            NSRect rect;

            // draw index for non-group items
            if (!item.IsGroup)
            {
                NSString index = NSString.StringWithUTF8String(string.Format("{0}. ", item.Index + 1));

                size = index.SizeWithAttributes(titleAttr);
                rect = new NSRect(aCellFrame.MinX, vertPos, size.width, size.height);
                index.DrawInRectWithAttributes(rect, titleAttr);

                titleIndent = size.width;
            }

            // draw the title
            NSString title = NSString.StringWithUTF8String(item.Title);

            size = title.SizeWithAttributes(titleAttr);
            rect = new NSRect(aCellFrame.MinX + titleIndent, vertPos, aCellFrame.Width - titleIndent, size.height);
            title.DrawInRectWithAttributes(rect, titleAttr);

            vertPos += size.height;

            // draw subtitle 1 aligned with the left of the cell
            if (item.Subtitle1 != string.Empty)
            {
                NSString subtitle1 = NSString.StringWithUTF8String(item.Subtitle1);

                size = subtitle1.SizeWithAttributes(subtitleAttr);
                rect = new NSRect(aCellFrame.MinX, vertPos, aCellFrame.Width, size.height);
                subtitle1.DrawInRectWithAttributes(rect, subtitleAttr);

                vertPos += size.height;
            }

            // draw subtitle 2 aligned with the left of the cell
            if (item.Subtitle2 != string.Empty)
            {
                NSString subtitle2 = NSString.StringWithUTF8String(item.Subtitle2);

                size = subtitle2.SizeWithAttributes(subtitleAttr);
                rect = new NSRect(aCellFrame.MinX, vertPos, aCellFrame.Width, size.height);
                subtitle2.DrawInRectWithAttributes(rect, subtitleAttr);
            }
        }