using System; using System.Drawing; using System.ComponentModel; #if __UNIFIED__ using UIKit; using CoreText; #else using MonoTouch.UIKit; using MonoTouch.CoreText; #endif #if __UNIFIED__ using RectangleF = CoreGraphics.CGRect; using SizeF = CoreGraphics.CGSize; using PointF = CoreGraphics.CGPoint; #else using nfloat=System.Single; using nint=System.Int32; using nuint=System.UInt32; #endif namespace Xamarin.Forms.Platform.iOS { public class LabelRenderer : ViewRenderer { SizeRequest _perfectSize; bool _perfectSizeValid; public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint) { if (!_perfectSizeValid) { _perfectSize = base.GetDesiredSize(double.PositiveInfinity, double.PositiveInfinity); _perfectSize.Minimum = new Size(Math.Min(10, _perfectSize.Request.Width), _perfectSize.Request.Height); _perfectSizeValid = true; } if (widthConstraint >= _perfectSize.Request.Width && heightConstraint >= _perfectSize.Request.Height) return _perfectSize; var result = base.GetDesiredSize(widthConstraint, heightConstraint); result.Minimum = new Size(Math.Min(10, result.Request.Width), result.Request.Height); if ((Element.LineBreakMode & (LineBreakMode.TailTruncation | LineBreakMode.HeadTruncation | LineBreakMode.MiddleTruncation)) != 0) { if (result.Request.Width > widthConstraint) result.Request = new Size(Math.Max(result.Minimum.Width, widthConstraint), result.Request.Height); } return result; } public override void LayoutSubviews() { base.LayoutSubviews(); if (Control == null) return; SizeF fitSize; nfloat labelHeight; switch (Element.VerticalTextAlignment) { case TextAlignment.Start: fitSize = Control.SizeThatFits(Element.Bounds.Size.ToSizeF()); labelHeight = (nfloat)Math.Min(Bounds.Height, fitSize.Height); Control.Frame = new RectangleF(0, 0, (nfloat)Element.Width, labelHeight); break; case TextAlignment.Center: Control.Frame = new RectangleF(0, 0, (nfloat)Element.Width, (nfloat)Element.Height); break; case TextAlignment.End: nfloat yOffset = 0; fitSize = Control.SizeThatFits(Element.Bounds.Size.ToSizeF()); labelHeight = (nfloat)Math.Min(Bounds.Height, fitSize.Height); yOffset = (nfloat)(Element.Height - labelHeight); Control.Frame = new RectangleF(0, yOffset, (nfloat)Element.Width, labelHeight); break; } } protected override void OnElementChanged(ElementChangedEventArgs