I am trying to place Label under the image inside a button and center it. I have a custom renderer for the button but for some reason I can not produce the desired output. The code is in C# Xamarin.
XAML Code:
<Controls:ExtendedButton
VerticalOptions="EndAndExpand"
HorizontalOptions="CenterAndExpand"
x:Name="search"
Text="Search"
Image="Completed.png"
IsEnabled="{Binding IsLoading}"
Command="{Binding SearchCommand}"
WidthRequest ="120"
HeightRequest ="120"
TextColor="#FFFFFF"
>
</Controls:ExtendedButton>
C# iOS CustomRenderer
public class ExtendedButtonRenderer : ButtonRenderer
{
UIButton btn;
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (e.OldElement == null)
{
btn = (UIButton)Control;
CGRect imageFrame = btn.ImageView.Frame;
imageFrame.Y = 0;
imageFrame.X = (btn.Frame.Size.Width / 2) - (imageFrame.Size.Width / 2 );
btn.ImageView.Frame = imageFrame;
var labelSize = new NSString(btn.TitleLabel.Text).
GetBoundingRect(
new CGSize(btn.Frame.Width, float.MaxValue),
NSStringDrawingOptions.UsesLineFragmentOrigin,
new UIStringAttributes() { Font = UIFont.SystemFontOfSize(8) },
null);
CGRect titleLabelFrame = new CGRect(labelSize.X, labelSize.Y, labelSize.Width, labelSize.Height);
titleLabelFrame.X = (btn.TitleLabel.Frame.Size.Width / 2) - (labelSize.Width / 2);
titleLabelFrame.Y = (btn.ImageView.Frame.Y ) + (btn.ImageView.Frame.Size.Height) + 20;
btn.TitleLabel.Frame = titleLabelFrame;
}
}
DesiredOutput
+-----------+
| |
| (Image) |
| Label |
| |
+-----------+