コード例 #1
0
 // Standard UIView drawRect override that uses Core Text to draw our text contents
 public override void Draw(CGRect rect)
 {
     // First draw selection / marked text, then draw text
     DrawRangeAsSelection(SelectedTextRange);
     DrawRangeAsSelection(MarkedTextRange);
     frame.Draw(UIGraphics.GetCurrentContext());
 }
コード例 #2
0
ファイル: DailyGraphView.cs プロジェクト: blparr/XWeather
        void drawLabel(CGContext ctx, CGRect rect, nfloat yCoord, nfloat xCoord, UITextAlignment alignment, string label, bool flipContext = true, bool centerVertical = false)
        {
            // Draw light the sunrise and Sunset labels at the ends of the light box
            using (UIColor fontColor = UIColor.White, shadowColor = UIColor.Black.ColorWithAlpha(0.1f)) {
                var fontAttributes = new UIFontAttributes(new UIFontFeature(CTFontFeatureNumberSpacing.Selector.ProportionalNumbers));

                using (var desc = UIFont.SystemFontOfSize(fontSize).FontDescriptor.CreateWithAttributes(fontAttributes)) {
                    using (UIFont font = UIFont.FromDescriptor(desc, fontSize)) {
                        // calculating the range of our attributed string
                        var range = new NSRange(0, label.Length);

                        // set justification for text block
                        using (var alignStyle = new NSMutableParagraphStyle {
                            Alignment = alignment
                        }) {
                            // add stylistic attributes to out attributed string
                            var stringAttributes = new UIStringAttributes {
                                ForegroundColor = fontColor,
                                Font            = font,
                                ParagraphStyle  = alignStyle
                            };

                            var target = new CGSize(float.MaxValue, float.MaxValue);

                            NSRange fit;

                            using (NSMutableAttributedString attrString = new NSMutableAttributedString(label, stringAttributes)) {
                                //creating a container for out attributed string
                                using (CTFramesetter framesetter = new CTFramesetter(attrString)) {
                                    CGSize frameSize = framesetter.SuggestFrameSize(range, null, target, out fit);


                                    if (alignment == UITextAlignment.Center)
                                    {
                                        xCoord -= (frameSize.Width / 2);
                                    }

                                    if (alignment == UITextAlignment.Right)
                                    {
                                        xCoord -= frameSize.Width;
                                    }


                                    // subtract the frameSize so the flipped context behaves as expected
                                    yCoord -= frameSize.Height;

                                    if (centerVertical)
                                    {
                                        yCoord += (frameSize.Height / 2);
                                    }


                                    var textRect = new CGRect(xCoord, yCoord, frameSize.Width, frameSize.Height);

                                    using (CGPath path = new CGPath()) {
                                        path.AddRect(textRect);

                                        ctx.SetShadow(new CGSize(0.0f, 1.0f), 0.0f, shadowColor.CGColor);

                                        using (CTFrame frame = framesetter.GetFrame(range, path, null)) {
                                            if (flipContext)
                                            {
                                                ctx.SaveState();
                                                ctx.TranslateCTM(0, rect.Height);
                                                ctx.ScaleCTM(1, -1);

                                                frame.Draw(ctx);

                                                ctx.RestoreState();
                                            }
                                            else
                                            {
                                                frame.Draw(ctx);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }