[iOS] Fix the layout of the button (#5221) * fixes #5160
authorMatthew Leibowitz <mattleibow@live.com>
Tue, 19 Feb 2019 12:36:59 +0000 (14:36 +0200)
committerRui Marinho <me@ruimarinho.net>
Tue, 19 Feb 2019 12:36:59 +0000 (12:36 +0000)
- it appears that iOS has a nice little delay when setting the title to the layout

Xamarin.Forms.Controls/GalleryPages/ButtonLayoutGalleryPage.xaml
Xamarin.Forms.Platform.iOS/Renderers/ButtonLayoutManager.cs

index 51ec220..1b7417d 100644 (file)
                                <Button Text="Some Text" />
                                <Button Image="bank.png" />
                                <Button Text="Some Text" Image="bank.png" />
+                               <Button Text="Some Text" Image="bank.png" ContentLayout="Top, 8" />
+                               <Button Text="Some Text" Image="bank.png" ContentLayout="Right, 8" />
+                               <Button Text="Some Text" Image="bank.png" ContentLayout="Bottom, 8" />
+                               <Button Text="Some Text" Image="bank.png" ContentLayout="Left, 8" />
                        </StackLayout>
 
                        <Label Text="Autosized:" Margin="0,0,0,-5" />
index a18d58d..a45b8db 100644 (file)
@@ -2,6 +2,7 @@
 using System.ComponentModel;
 using System.Threading.Tasks;
 using CoreGraphics;
+using Foundation;
 using UIKit;
 
 namespace Xamarin.Forms.Platform.iOS
@@ -263,9 +264,15 @@ namespace Xamarin.Forms.Platform.iOS
                        {
                                // TODO: Do not use the title label as it is not yet updated and
                                //       if we move the image, then we technically have more
-                               //       space and will require a new laoyt pass.
-
-                               var titleRect = control.TitleLabel.Bounds.Size;
+                               //       space and will require a new layout pass.
+
+                               var title =
+                                       control.CurrentAttributedTitle ??
+                                       new NSAttributedString(control.CurrentTitle, new UIStringAttributes { Font = control.TitleLabel.Font });
+                               var titleRect = title.GetBoundingRect(
+                                       control.Bounds.Size,
+                                       NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading,
+                                       null);
 
                                var titleWidth = titleRect.Width;
                                var titleHeight = titleRect.Height;