Пример #1
0
        // ReSharper disable once MethodOverloadWithOptionalParameter
        public double CalculateHeight(string text, double width, float textSize, string fontName = null)
        {
            //uiLabel = new UILabel
            //{
            //    Text = text,
            //};
            //var font = uiLabel.Font;
            //font = font.WithSize(textSize);
            //uiLabel.Font = font;
            //uiLabel.SetNeedsLayout();
            //return uiLabel.Frame.Size.Height;

            var nsText    = new NSString(text);
            var boundSize = new SizeF((float)width, float.MaxValue);
            var options   = NSStringDrawingOptions.UsesFontLeading |
                            NSStringDrawingOptions.UsesLineFragmentOrigin;

            if (fontName == null)
            {
                fontName = "HelveticaNeue";
            }

            var attributes = new UIStringAttributes
            {
                Font = UIFont.FromName(fontName, textSize)
            };

            var sizeF = nsText.GetBoundingRect(boundSize, options, attributes, null).Size;

            return(sizeF.Height);
        }
Пример #2
0
        public static float ContentSize(UITextView textView)
        {
            var frame = textView.Bounds;

            var textContainerInsets = textView.TextContainerInset;
            var contentInsents = textView.ContentInset;

            var leftRightPadding = textContainerInsets.Left + textContainerInsets.Right + textView.TextContainer.LineFragmentPadding * 2 + contentInsents.Left + contentInsents.Right;
            var topBottomPadding = textContainerInsets.Top + textContainerInsets.Bottom + contentInsents.Top + contentInsents.Bottom;

            var width = frame.Size.Width;
            width -= leftRightPadding;

            var height = frame.Size.Height;
            height -= topBottomPadding;

            frame.Size = new SizeF (width, height);

            var text = new NSString (textView.Text);

            var attributes = new UIStringAttributes {
                Font = textView.Font
            };

            var size = text.GetBoundingRect (new SizeF (frame.Width, float.MaxValue), NSStringDrawingOptions.UsesLineFragmentOrigin, attributes, new NSStringDrawingContext ());
            float measuredHeight = size.Height + topBottomPadding;
            return measuredHeight;
        }
Пример #3
0
        public static CGSize CalculateTextSize(CGSize frame, string text, UIFont font)
        {
            if (!string.IsNullOrEmpty(text))
            {
                CGRect textSize;

                using (var ns = new NSString(text))
                {
                    textSize = ns.GetBoundingRect(
                        frame,
                        NSStringDrawingOptions.UsesLineFragmentOrigin |
                        NSStringDrawingOptions.UsesFontLeading,
                        new UIStringAttributes {
                        Font = font
                    },
                        null);
                }

                return(new CGSize(
                           (float)Math.Ceiling(textSize.Width),
                           (float)Math.Ceiling(textSize.Height)));
            }

            return(CGSize.Empty);
        }
Пример #4
0
        public static void SizeHeaderToFitLabels(this UITableView tableView, List <UILabel> labels, bool ignoreNewLineCharacters = true, float offset = 0f)
        {
            var currentFrame = tableView.TableHeaderView.Frame;

            nfloat sumHeight = 0f;

            foreach (UILabel label in labels)
            {
                label.Text = FormatString(ignoreNewLineCharacters, label.Text);
                var nstext          = new NSString(label.Text);
                var firstAttributes = new UIStringAttributes {
                    Font = label.Font,
                };
                var temp = nstext.GetBoundingRect(new CGSize(label.Frame.Width, 0), NSStringDrawingOptions.UsesLineFragmentOrigin, firstAttributes, null);
                sumHeight += temp.Height;
            }

            var viewsThatAreAlighedToTheLeftOfTheScreen = tableView.TableHeaderView.Subviews.Where(v => v.Frame.Left <= (tableView.TableHeaderView.Frame.GetMidX() / 2f)).OrderBy(v => v.Frame.Top).ToList();

            var otherViews       = viewsThatAreAlighedToTheLeftOfTheScreen.Where(v => !labels.Contains(v)).ToList();
            var otherViewsHeight = otherViews.Sum(v => v.Frame.Height);

            var spacing = currentFrame.Height - (viewsThatAreAlighedToTheLeftOfTheScreen.Sum(v => v.Frame.Height));

            var finalHeight = (float)((double)spacing + otherViewsHeight + (double)sumHeight) + offset;

            currentFrame.Height = finalHeight;

            tableView.TableHeaderView.Frame = currentFrame;
        }
Пример #5
0
        private static void ProcessSubtitle(
            this AVPlayerViewController controller,
            CMTime time)
        {
            NSString text = NSString.Empty;

            NSArray payload = controller.Payload();

            for (nuint i = 0; i < payload.Count; ++i)
            {
                NSDictionary item = payload.GetItem <NSDictionary>(i);
                NSNumber     from = item.ObjectForKey(new NSString("from")) as NSNumber;
                NSNumber     to   = item.ObjectForKey(new NSString("to")) as NSNumber;
                if (time.Seconds >= from.FloatValue &&
                    time.Seconds < to.FloatValue)
                {
                    text = item.ObjectForKey(new NSString("text")) as NSString;
                    break;
                }
            }

            UILabel subtitleLabel = controller.SubtitleLabel();

            subtitleLabel.Text = text;
            var attributes = new UIStringAttributes();

            attributes.Font = subtitleLabel.Font;
            CGRect rect = text.GetBoundingRect(
                new CGSize(subtitleLabel.Bounds.Width, nfloat.MaxValue),
                NSStringDrawingOptions.UsesLineFragmentOrigin,
                attributes, null
                );

            controller.SubtitleConstranint().Constant = rect.Size.Height + 5;
        }
Пример #6
0
        public override Xamarin.Forms.Size Measure(string text, double fontSize, string fontName)
        {
            UIFont font = this.getFont(fontName, fontSize);

            UIStringAttributes attributes = new UIStringAttributes();

            attributes.Font = font;

            SizeF boundSize = new SizeF(float.PositiveInfinity, float.PositiveInfinity);
            NSStringDrawingOptions options = NSStringDrawingOptions.UsesFontLeading |
                                             NSStringDrawingOptions.UsesLineFragmentOrigin;

            NSString nsText     = new NSString(text);
            CGSize   resultSize = nsText.GetBoundingRect(
                boundSize,
                options,
                attributes,
                null).Size;

            nsText.Dispose();

            return(new Xamarin.Forms.Size(
                       Math.Ceiling((double)resultSize.Width),
                       Math.Ceiling((double)resultSize.Height)));
        }
        private static void GetFontSize(UILabel label, CGSize size, int maxFontSize, int minFontSize)
        {
            label.Frame = new CGRect(CGPoint.Empty, size);
            var fontSize       = maxFontSize;
            var constraintSize = new CGSize(label.Frame.Width, nfloat.MaxValue);

            while (fontSize > minFontSize)
            {
                label.Font = UIFont.FromName(label.Font.Name, fontSize);
                using (var nativeString = new NSString(label.Text))
                {
                    var textRect = nativeString.GetBoundingRect(
                        constraintSize,
                        NSStringDrawingOptions.UsesFontLeading,
                        new UIStringAttributes {
                        Font = label.Font
                    },
                        null
                        );

                    if (textRect.Size.Height <= label.Frame.Height)
                    {
                        break;
                    }
                }

                fontSize -= 2;
            }
        }
Пример #8
0
        public static float ContentSize(UITextView textView)
        {
            var frame = textView.Bounds;

            var textContainerInsets = textView.TextContainerInset;
            var contentInsents      = textView.ContentInset;

            var leftRightPadding = textContainerInsets.Left + textContainerInsets.Right + textView.TextContainer.LineFragmentPadding * 2 + contentInsents.Left + contentInsents.Right;
            var topBottomPadding = textContainerInsets.Top + textContainerInsets.Bottom + contentInsents.Top + contentInsents.Bottom;

            frame.Size.Width  -= leftRightPadding;
            frame.Size.Height -= topBottomPadding;

            var text           = new NSString(textView.Text);
            var paragraphStyle = new NSMutableParagraphStyle {
                LineBreakMode = UILineBreakMode.WordWrap,
            };

            var attributes = new UIStringAttributes {
                Font = textView.Font
            };

            var   size           = text.GetBoundingRect(new SizeF(frame.Width, float.MaxValue), NSStringDrawingOptions.UsesLineFragmentOrigin, attributes, new NSStringDrawingContext());
            float measuredHeight = size.Height + topBottomPadding;

            return(measuredHeight);
        }
        private void UpdateTextSizing()
        {
            if (String.IsNullOrEmpty(_materialButton.Text) || !_withIcon)
            {
                this.Control.TitleEdgeInsets = new UIEdgeInsets(0, 0, 0, 0);
                return;
            }

            // We have to set the button title insets to make the button look
            // like Android's material buttons. (icon on left, text is centralized)
            //
            NSString textToMeasure = (NSString)(_materialButton.Text ?? "");

            CGRect labelRect = textToMeasure.GetBoundingRect(
                new CGSize(this.Frame.Width - 40, nfloat.MaxValue),
                NSStringDrawingOptions.UsesLineFragmentOrigin,
                new UIStringAttributes()
            {
                Font = this.Control.Font
            },
                new NSStringDrawingContext()
                );

            float textWidth   = (float)labelRect.Size.Width;
            float buttonWidth = (float)this.Control.Frame.Width;

            float inset = ((buttonWidth - textWidth) / 2) - 28;

            this.Control.TitleEdgeInsets = new UIEdgeInsets(0, inset, 0, -40);
        }
        private static void GetFontSize(UILabel label, CGSize size, int maxFontSize, int minFontSize)
        {
            label.Frame = new CGRect(CGPoint.Empty, size);
              var fontSize = maxFontSize;
              var constraintSize = new CGSize(label.Frame.Width, nfloat.MaxValue);
              while (fontSize > minFontSize)
              {
            label.Font = UIFont.FromName(label.Font.Name, fontSize);
            using (var nativeString = new NSString(label.Text))
            {
              var textRect = nativeString.GetBoundingRect(
            constraintSize,
            NSStringDrawingOptions.UsesFontLeading,
            new UIStringAttributes { Font = label.Font},
            null
              );

              if (textRect.Size.Height <= label.Frame.Height)
              {
            break;
              }
            }

            fontSize -= 2;
              }
        }
Пример #11
0
        private UIImage CreateImage (NSString title, float scale)
        {
            var titleAttrs = new UIStringAttributes () {
                Font = UIFont.FromName ("HelveticaNeue", 13f),
                ForegroundColor = Color.Gray,
            };

            var titleBounds = title.GetBoundingRect (
                                  new SizeF (Single.PositiveInfinity, Single.PositiveInfinity),
                                  NSStringDrawingOptions.UsesFontLeading | NSStringDrawingOptions.UsesDeviceMetrics,
                                  titleAttrs,
                                  null);

            var image = Image.TagBackground;
            var imageBounds = new RectangleF (
                                  0, 0,
                                  (float)Math.Ceiling (titleBounds.Width) + image.CapInsets.Left + image.CapInsets.Right + 4f,
                                  (float)Math.Ceiling (titleBounds.Height) + image.CapInsets.Top + image.CapInsets.Bottom
                              );

            titleBounds.X = image.CapInsets.Left + 2f;
            titleBounds.Y = image.CapInsets.Top;

            UIGraphics.BeginImageContextWithOptions (imageBounds.Size, false, scale);

            try {
                image.Draw (imageBounds);
                title.DrawString (titleBounds, titleAttrs);
                return UIGraphics.GetImageFromCurrentImageContext ();
            } finally {
                UIGraphics.EndImageContext ();
            }
        }
		public override float GetHeightForRow (UITableView tableView, NSIndexPath indexPath)
		{
			FeedItem comment = FeedItems [indexPath.Item];
			NSString caption = new NSString (comment.Caption);

			RectangleF rect = caption.GetBoundingRect (new SizeF (150, 0), NSStringDrawingOptions.UsesLineFragmentOrigin,
				                  new UIStringAttributes () { }, null);

			return Math.Max (55, rect.Height);
		}
Пример #13
0
        public static CGSize StringSize(this UIFont font, NSString String, double width)
        {
            var attributes = new UIStringAttributes();

            attributes.Font = font;
            return(String.GetBoundingRect(new CGSize(width, double.MaxValue),
                                          options: NSStringDrawingOptions.UsesLineFragmentOrigin,
                                          attributes: attributes,
                                          context: null).Size);
        }
Пример #14
0
        /// <summary>
        /// Gets the rectangle of a string.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="font">The font.</param>
        /// <param name="width">The width.</param>
        /// <returns>CGRect.</returns>
        public static CGRect StringRect(this string text, UIFont font, nfloat width)
        {
            var nativeString = new NSString(text);

            return nativeString.GetBoundingRect(
                new CGSize(width, float.MaxValue),
                NSStringDrawingOptions.UsesLineFragmentOrigin,
                new UIStringAttributes { Font = font },
                null);
        }
Пример #15
0
        public static nfloat GetHeightForMultilineLabelWithString(this string str, nfloat width, UIFont font)
        {
            var    nativeString = new NSString(str);
            CGSize maxLabelSize = new CGSize(width, float.MaxValue);

            CGRect textRect = nativeString.GetBoundingRect(maxLabelSize, NSStringDrawingOptions.UsesLineFragmentOrigin, new UIStringAttributes {
                Font = font
            }, null);

            return(textRect.Size.Height);
        }
Пример #16
0
        /// <summary>
        /// Gets the rectangle of a string.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="font">The font.</param>
        /// <param name="width">The width.</param>
        /// <returns>CGRect.</returns>
        public static CGRect StringRect(this string text, UIFont font, nfloat width)
        {
            var nativeString = new NSString(text);

            return(nativeString.GetBoundingRect(
                       new CGSize(width, float.MaxValue),
                       NSStringDrawingOptions.UsesLineFragmentOrigin,
                       new UIStringAttributes {
                Font = font
            },
                       null));
        }
        public static float StringHeight(this string text, UIFont font, float width)
        {
            var nativeString = new NSString(text);

            var rect = nativeString.GetBoundingRect(
                new System.Drawing.SizeF(width, float.MaxValue),
                NSStringDrawingOptions.UsesLineFragmentOrigin,
                new UIStringAttributes() { Font = font },
                null);

            return rect.Height;
        }
Пример #18
0
        private CGSize SizeOfString(NSString text)
        {
            CGSize constraint = new CGSize(1000, 1000);
            CGSize textSize   = text.GetBoundingRect(constraint,
                                                     options: NSStringDrawingOptions.UsesLineFragmentOrigin,
                                                     attributes: new UIStringAttributes {
                Font = UIFont.BoldSystemFontOfSize(this.FontSize)
            },
                                                     context: null).Size;

            textSize.Width += text.Length * 2;
            return(textSize);
        }
Пример #19
0
        private CGSize CalculateStringSize(string note, UIFont font)
        {
            CGSize constraint = new CGSize(paperWidth - (paperWidth / 1.7f) - points_per_inch, nfloat.MaxValue);

            NSString           str         = new NSString(note);
            UIStringAttributes attributes1 = new UIStringAttributes();

            attributes1.Font = font;

            var rect = str.GetBoundingRect(constraint, NSStringDrawingOptions.UsesLineFragmentOrigin, attributes1, null);

            return(rect.Size);
        }
 private nfloat TextHeight(CGRect bounds, string text, UIFont font)
 {
     using (NSString str = new NSString(text))
     {
         return(str.GetBoundingRect(new CGSize(bounds.Width - 20, 1000), NSStringDrawingOptions.UsesLineFragmentOrigin, new UIStringAttributes()
         {
             Font = font,
             ParagraphStyle = new NSMutableParagraphStyle()
             {
                 LineBreakMode = UILineBreakMode.WordWrap,
             },
         }, null).Height);
     }
 }
        /// <summary>
        /// Strings the height.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="font">The font.</param>
        /// <param name="width">The width.</param>
        /// <returns>System.Single.</returns>
        public static float StringHeight(this string text, UIFont font, float width)
        {
            var nativeString = new NSString(text);

            var rect = nativeString.GetBoundingRect(
                new SizeF(width, float.MaxValue),
                NSStringDrawingOptions.UsesLineFragmentOrigin,
                new UIStringAttributes {
                Font = font
            },
                null);

            return(rect.Height);
        }
Пример #22
0
 private nfloat TextHeight(RectangleF bounds)
 {
     using (NSString str = new NSString(this.Subject))
     {
         return(str.GetBoundingRect(new SizeF(bounds.Width - 20, 1000), NSStringDrawingOptions.UsesLineFragmentOrigin, new UIStringAttributes()
         {
             Font = subjectFont,
             ParagraphStyle = new NSMutableParagraphStyle()
             {
                 LineBreakMode = UILineBreakMode.WordWrap,
             },
         }, null).Height);
     }
 }
Пример #23
0
        float GetStringHeight(string text, UIFont font, float width)
        {
            var nativeString = new NSString(text);

            var rect = nativeString.GetBoundingRect(
                new System.Drawing.SizeF(width, float.MaxValue),
                NSStringDrawingOptions.UsesLineFragmentOrigin,
                new UIStringAttributes()
            {
                Font = font
            },
                null);

            return((float)rect.Height);
        }
Пример #24
0
        public static nfloat StringHeight(this string text, UIFont font, nfloat width)
        {
            var nativeString = new NSString(text);

            var rect = nativeString.GetBoundingRect(
                new CGSize(width, float.MaxValue),
                NSStringDrawingOptions.OneShot,
                new UIStringAttributes()
            {
                Font = font
            },
                null);

            return(rect.Height);
        }
        private nfloat GetHeightFromText(string text)
        {
            var explanation = new NSString(text);

            var textSize = new CGSize(txtExplain.Frame.Width, 1000);
            var size     = explanation.GetBoundingRect(textSize,
                                                       NSStringDrawingOptions.UsesLineFragmentOrigin,
                                                       new UIStringAttributes()
            {
                Font = UIFont.FromName("HelveticaNeue", 14f)
            },
                                                       null);

            return(size.Height);
        }
Пример #26
0
        public Size Measure(FormattedText formattedText)
        {
            var width   = formattedText.Constraint.Width;
            var options = NSStringDrawingOptions.UsesFontLeading |
                          NSStringDrawingOptions.UsesLineFragmentOrigin;

            var boundSize  = new SizeF((float)width, float.MaxValue);
            var nsText     = new NSString(formattedText.Text);
            var attributes = new UIStringAttributes
            {
                Font = UIFont.FromName(formattedText.FontName, formattedText.FontSize),
            };
            var sizeF = nsText.GetBoundingRect(boundSize, options, attributes, null).Size;

            return(sizeF.ToOmniGui());
        }
Пример #27
0
        public static SizeF MeasureTextSize(this string text, double width, double fontsize, string fontName = "HelveticaNeue")
        {
            var nsText    = new NSString(text);
            var boundSize = new SizeF((float)width, float.MaxValue);

            var options = NSStringDrawingOptions.UsesFontLeading | NSStringDrawingOptions.UsesLineFragmentOrigin;

            var attributes = new UIStringAttributes
            {
                Font = UIFont.FromName(fontName, (float)fontsize)
            };

            var size = nsText.GetBoundingRect(boundSize, options, attributes, null).Size;

            return(new SizeF((float)size.Width, (float)size.Height));
        }
Пример #28
0
        //
        // Summary:
        //     Calculates the dimensions of the text of given font
        //
        // Parameters:
        //   font:
        //     The font used to draw the text
        //   text:
        //     The text string to be measured
        //   height:
        //     The max. height of the text. Single line
        //
        // Remarks:
        //     This method emulates drawing the text string with one single
        //     line and return the width and height sufficient to accomodate
        //     the given string draw with the given font
        //
        // Returns:
        //     Returns the bounding rectangle for drawing the given string
        //
        private CGSize measureText(UIFont font, string text, nfloat height)
        {
            var nsText    = new NSString(text);
            var boundSize = new CGSize(float.MaxValue, height);
            var options   = NSStringDrawingOptions.UsesFontLeading |
                            NSStringDrawingOptions.UsesLineFragmentOrigin;

            var attributes = new UIStringAttributes
            {
                Font = font
            };

            var sizeF = nsText.GetBoundingRect(boundSize, options, attributes, null).Size;

            return(sizeF);
        }
Пример #29
0
        public void desenharTextoLabel(string Texto, float x, float y, PonteiroCorEnum cor)
        {
            var currentContext = UIGraphics.GetCurrentContext();

            currentContext.SelectFont("Arial", 22f, CGTextEncoding.MacRoman);
            currentContext.SetTextDrawingMode(CGTextDrawingMode.Fill);
            currentContext.SetFillColor(pegarCor(cor));
            var nsText    = new NSString(Texto);
            var boundSize = new SizeF((float)x, float.MaxValue);
            var options   = NSStringDrawingOptions.UsesFontLeading |
                            NSStringDrawingOptions.UsesLineFragmentOrigin;


            var attributes = new UIStringAttributes
            {
                Font = UIFont.FromName("Arial", (float)22)
            };

            var sizeF = nsText.GetBoundingRect(boundSize, options, attributes, null).Size;

            if (TelaUtils.Orientacao == "LandscapeLeft" || TelaUtils.Orientacao == "LandscapeRight")
            {
                if (TelaUtils.Dispositivo == "Pad")
                {
                    currentContext.ShowTextAtPoint((this.pegarLarguraTela() - sizeF.Width) / 3, y - 60, Texto);
                }
                else
                {
                    currentContext.ShowTextAtPoint((this.pegarLarguraTela() - sizeF.Width) / 3, y - 65, Texto);
                }
            }
            else
            {
                if (TelaUtils.Dispositivo == "Pad")
                {
                    currentContext.ShowTextAtPoint((this.pegarLarguraTela() - sizeF.Width) / 2, y - 300, Texto);
                }
                else
                {
                    currentContext.ShowTextAtPoint((this.pegarLarguraTela() - sizeF.Width) / 2, y - 35, Texto);
                }
            }


            currentContext.DrawPath(CoreGraphics.CGPathDrawingMode.FillStroke);
        }
Пример #30
0
        public void DrawingExtensions()
        {
            TestRuntime.AssertXcodeVersion(5, 0);

            using (var s = new NSString("foo")) {
                NSStringDrawingOptions options = NSStringDrawingOptions.OneShot;
                var attrib = new UIStringAttributes();
                using (var dict = new NSDictionary()) {
                    Assert.DoesNotThrow(() => s.GetBoundingRect(new SizeF(5, 5), options, attrib, null), "GetBoundingRect 1");
                    Assert.DoesNotThrow(() => s.WeakGetBoundingRect(new SizeF(5, 5), options, dict, null), "WeakGetBoundingRect 1");
                    Assert.DoesNotThrow(() => s.DrawString(new RectangleF(0, 0, 10, 10), options, attrib, null), "DrawString 1");
                    Assert.DoesNotThrow(() => s.WeakDrawString(new RectangleF(0, 0, 10, 10), options, dict, null), "WeakDrawString 1");
                    Assert.DoesNotThrow(() => s.WeakDrawString(new RectangleF(0, 0, 10, 10), dict), "WeakDrawString 2");
                    Assert.DoesNotThrow(() => s.WeakDrawString(new PointF(0, 0), dict), "WeakDrawString 3");
                }
            }
        }
        public static nfloat HeightForFont(UIFont font, string s, nfloat width)
        {
            CGSize   availableSize = new CGSize(width, float.MaxValue);
            NSString ns            = new NSString(s);

            return(ns.GetBoundingRect(
                       availableSize,
                       NSStringDrawingOptions.UsesLineFragmentOrigin,
                       new UIStringAttributes
            {
                ParagraphStyle = new NSMutableParagraphStyle {
                    LineBreakMode = UILineBreakMode.WordWrap
                },
                Font = font
            },
                       context: null).Height);
        }
Пример #32
0
        public Xamarin.Forms.Size GetTextSize(
            string text,
            double maxWidth,
            double fontSize = 0,
            string fontName = null)
        {
            if (fontSize <= 0)
            {
                fontSize = UIFont.SystemFontSize;
            }

            UIFont font = null;

            if (string.IsNullOrEmpty(fontName))
            {
                font = UIFont.SystemFontOfSize((nfloat)fontSize);
            }
            else
            {
                font = UIFont.FromName(fontName, (nfloat)fontSize);
            }

            var attributes = new UIStringAttributes {
                Font = font
            };
            var boundSize = new SizeF((float)maxWidth, float.MaxValue);
            var options   = NSStringDrawingOptions.UsesFontLeading |
                            NSStringDrawingOptions.UsesLineFragmentOrigin;

            var nsText     = new NSString(text);
            var resultSize = nsText.GetBoundingRect(
                boundSize,
                options,
                attributes,
                null).Size;

            font.Dispose();
            nsText.Dispose();

            return(new Xamarin.Forms.Size(
                       Math.Ceiling((double)resultSize.Width),
                       Math.Ceiling((double)resultSize.Height)));
        }
Пример #33
0
        private CGSize MeasureTextSize(string text, double width,
                                       double fontSize, string fontName = null)
        {
            var nsText    = new NSString(text);
            var boundSize = new SizeF((float)width, float.MaxValue);
            var options   = NSStringDrawingOptions.UsesFontLeading |
                            NSStringDrawingOptions.UsesLineFragmentOrigin;

            if (fontName == null)
            {
                fontName = "HelveticaNeue";
            }

            var attributes = new UIStringAttributes {
                Font = UIFont.FromName(fontName, (float)fontSize)
            };

            return(nsText.GetBoundingRect(boundSize, options, attributes, null).Size);
        }
Пример #34
0
        public double MeasureTextSize(string text, double width, double fontSize, string fontName = null)
        {
            var nsText    = new NSString(text);
            var boundSize = new SizeF((float)width, float.MaxValue);
            var options   = NSStringDrawingOptions.UsesFontLeading | NSStringDrawingOptions.UsesLineFragmentOrigin;

            if (fontName == null)
            {
                fontName = "HelveticaNeue";
            }
            var attributes = new UIStringAttributes
            {
                Font = UIFont.FromName(fontName, (float)fontSize)
            };
            var sizeF = nsText.GetBoundingRect(boundSize, options, attributes, null).Size;

            //return new Xamarin.Forms.Size((double)sizeF.Width, (double)sizeF.Height);
            return((double)sizeF.Height + 50);
        }
Пример #35
0
        public void DrawingExtensions()
        {
            if (!TestRuntime.CheckSystemAndSDKVersion(7, 0))
            {
                Assert.Inconclusive("requires iOS7+");
            }

            using (var s = new NSString("foo")) {
                NSStringDrawingOptions options = NSStringDrawingOptions.OneShot;
                var attrib = new UIStringAttributes();
                using (var dict = new NSDictionary()) {
                    Assert.DoesNotThrow(() => s.GetBoundingRect(new SizeF(5, 5), options, attrib, null), "GetBoundingRect 1");
                    Assert.DoesNotThrow(() => s.WeakGetBoundingRect(new SizeF(5, 5), options, dict, null), "WeakGetBoundingRect 1");
                    Assert.DoesNotThrow(() => s.DrawString(new RectangleF(0, 0, 10, 10), options, attrib, null), "DrawString 1");
                    Assert.DoesNotThrow(() => s.WeakDrawString(new RectangleF(0, 0, 10, 10), options, dict, null), "WeakDrawString 1");
                    Assert.DoesNotThrow(() => s.WeakDrawString(new RectangleF(0, 0, 10, 10), dict), "WeakDrawString 2");
                    Assert.DoesNotThrow(() => s.WeakDrawString(new PointF(0, 0), dict), "WeakDrawString 3");
                }
            }
        }
		public double MeasureTextSize(string text, double width, double fontSize, string fontName = null)
		{
			var nsText = new NSString(text);
			var boundSize = new SizeF((float)width, float.MaxValue);
			var options = NSStringDrawingOptions.UsesFontLeading | NSStringDrawingOptions.UsesLineFragmentOrigin;

			if (fontName == null)
			{
				fontName = "HelveticaNeue";
			}

			var attributes = new UIStringAttributes
			{
				Font = UIFont.FromName(fontName, (float)fontSize)
			};

			var sizeF = nsText.GetBoundingRect(boundSize, options, attributes, null).Size;

			return (double)sizeF.Height + 5;
		}
Пример #37
0
        public void DrawingExtensions()
        {
            TestRuntime.AssertXcodeVersion(5, 0);
            TestRuntime.AssertSystemVersion(PlatformName.MacOSX, 10, 11, throwIfOtherPlatform: false);

            using (var s = new NSString("foo")) {
                NSStringDrawingOptions options = NSStringDrawingOptions.OneShot;
                var attrib = new UIStringAttributes();
                using (var dict = new NSDictionary()) {
                    Assert.DoesNotThrow(() => s.GetBoundingRect(new CGSize(5, 5), options, attrib, null), "GetBoundingRect 1");
                    Assert.DoesNotThrow(() => s.WeakGetBoundingRect(new CGSize(5, 5), options, dict, null), "WeakGetBoundingRect 1");
                    Assert.DoesNotThrow(() => s.DrawString(new CGRect(0, 0, 10, 10), options, attrib, null), "DrawString 1");
                    Assert.DoesNotThrow(() => s.WeakDrawString(new CGRect(0, 0, 10, 10), options, dict, null), "WeakDrawString 1");
#if !MONOMAC //WeakDrawString on mac doesn't have versions with these parameters
                    Assert.DoesNotThrow(() => s.WeakDrawString(new CGRect(0, 0, 10, 10), dict), "WeakDrawString 2");
                    Assert.DoesNotThrow(() => s.WeakDrawString(new CGPoint(0, 0), dict), "WeakDrawString 3");
#endif
                }
            }
        }
        private CGRect AutoCompleteRectForBounds(CGRect bounds) 
        {
            // get bounds for whole text area
            CGRect textRectBounds = this.TextRect(bounds);

            if (this.BeginningOfDocument == null)
                return CGRect.Empty;

            // get rect for actual text
            UITextRange textRange = this.GetTextRange(this.BeginningOfDocument, this.EndOfDocument);

            CGRect textRect = this.GetFirstRectForRange(textRange).Integral();

            NSMutableParagraphStyle paragraphStyle = new NSMutableParagraphStyle();
            paragraphStyle.LineBreakMode = UILineBreakMode.CharacterWrap;

            NSString text = new NSString(this.Text);

            UIStringAttributes attributes = new UIStringAttributes();
            attributes.Font = this.Font;
            attributes.ParagraphStyle = paragraphStyle;

            CGRect prefixTextRect  = text.GetBoundingRect(
                textRect.Size, 
                NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading, 
                attributes, null
            );

            CGSize prefixTextSize = prefixTextRect.Size;

            NSString str = new NSString(autocompleteString);

            CGRect autocompleteTextRect = str.GetBoundingRect(
                new CGSize(textRectBounds.Size.Width - prefixTextSize.Width, textRectBounds.Size.Height),
                NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading, 
                attributes, null
            );

            CGSize autocompleteTextSize = autocompleteTextRect.Size;

            return new CGRect(textRect.GetMaxX() + 6, //6 - correction
                textRectBounds.GetMinY() - 1, //1 - correction
                autocompleteTextSize.Width,
                textRectBounds.Size.Height);
        }
Пример #39
0
		public static void SizeHeaderToFitLabels(this UITableView tableView, List<UILabel> labels, bool ignoreNewLineCharacters = true, float offset = 0f)
		{
			var currentFrame = tableView.TableHeaderView.Frame;

			nfloat sumHeight = 0f;
			foreach (UILabel label in labels)
			{
				label.Text = FormatString (ignoreNewLineCharacters, label.Text);
				var nstext = new NSString (label.Text);
				var firstAttributes = new UIStringAttributes {
					Font = label.Font,
				};
				var temp = nstext.GetBoundingRect (new CGSize (label.Frame.Width, 0), NSStringDrawingOptions.UsesLineFragmentOrigin, firstAttributes, null);
				sumHeight += temp.Height;
			}

			var viewsThatAreAlighedToTheLeftOfTheScreen = tableView.TableHeaderView.Subviews.Where (v=> v.Frame.Left <=  (tableView.TableHeaderView.Frame.GetMidX()/2f)).OrderBy(v=> v.Frame.Top).ToList();

			var otherViews = viewsThatAreAlighedToTheLeftOfTheScreen.Where(v=> !labels.Contains(v)).ToList();
			var otherViewsHeight = otherViews.Sum (v => v.Frame.Height);

			var spacing = currentFrame.Height - (viewsThatAreAlighedToTheLeftOfTheScreen.Sum(v=> v.Frame.Height));

			var finalHeight = (float) ((double)spacing + otherViewsHeight + (double)sumHeight) + offset;
			currentFrame.Height = finalHeight;

			tableView.TableHeaderView.Frame = currentFrame;
		}
		SizeF GetSizeForText (UIView tv, string text, bool splitLine){
			NSString nsText = new NSString (text);
			var textRect = nsText.GetBoundingRect (new SizeF (tv.Bounds.Width * .9f - 10 - 22, 99999), (splitLine ? NSStringDrawingOptions.UsesLineFragmentOrigin : 0), new UIStringAttributes { Font = font }, new NSStringDrawingContext ());
			return new SizeF (textRect.Width, textRect.Height);
		}
		private nfloat TextHeight (RectangleF bounds)
		{
			using (NSString str = new NSString (this.Subject))
			{
				return str.GetBoundingRect (new SizeF (bounds.Width - 20, 1000), NSStringDrawingOptions.UsesLineFragmentOrigin, new UIStringAttributes ()
				{
					Font = subjectFont,
					ParagraphStyle = new NSMutableParagraphStyle ()
					{
						LineBreakMode = UILineBreakMode.WordWrap,	
					},
				}, null).Height;
			}
		}