@@ -540,15 +540,20 @@ double CalculateWidth(double widthConstraint)
540
540
{
541
541
if ( widthConstraint == double . PositiveInfinity || widthConstraint < 0 || WidthRequest < 0 )
542
542
{
543
+ double buttonContentWidth = 0 ;
543
544
if ( ShowIcon && ImageSource != null )
544
545
{
545
- return ImageAlignment == Alignment . Top || ImageAlignment == Alignment . Bottom
546
+ buttonContentWidth = ImageAlignment == Alignment . Top || ImageAlignment == Alignment . Bottom
546
547
? Math . Max ( ImageSize , TextSize . Width ) + Padding . Left + Padding . Right + StrokeThickness + ( _leftPadding * 2 ) + ( _rightPadding * 2 )
547
548
: ImageSize + TextSize . Width + StrokeThickness + Padding . Left + Padding . Right + ( _leftPadding * 2 ) + ( _rightPadding * 2 ) ;
548
549
}
549
550
else
550
551
{
551
- return TextSize . Width + Padding . Left + Padding . Right + StrokeThickness + ( _leftPadding * 2 ) + ( _rightPadding * 2 ) ;
552
+ buttonContentWidth = TextSize . Width + Padding . Left + Padding . Right + StrokeThickness + ( _leftPadding * 2 ) + ( _rightPadding * 2 ) ;
553
+ }
554
+ if ( buttonContentWidth <= widthConstraint )
555
+ {
556
+ return buttonContentWidth ;
552
557
}
553
558
}
554
559
return widthConstraint ;
@@ -564,6 +569,7 @@ double CalculateHeight(double heightConstraint, double width)
564
569
{
565
570
if ( heightConstraint == double . PositiveInfinity || heightConstraint < 0 || VerticalOptions != LayoutOptions . Fill )
566
571
{
572
+ width = CalculateAvailableTextWidth ( width ) ;
567
573
if ( LineBreakMode == LineBreakMode . WordWrap || LineBreakMode == LineBreakMode . CharacterWrap )
568
574
{
569
575
_numberOfLines = StringExtensions . GetLinesCount ( Text , ( float ) width , this , LineBreakMode , out _ ) ;
@@ -587,6 +593,46 @@ double CalculateHeight(double heightConstraint, double width)
587
593
return heightConstraint ;
588
594
}
589
595
596
+ /// <summary>
597
+ /// Calculates the available width for the text area within the button.
598
+ /// </summary>
599
+ /// <param name="width"></param>
600
+ /// <returns></returns>
601
+ double CalculateAvailableTextWidth ( double width )
602
+ {
603
+ if ( ShowIcon && ImageSource != null )
604
+ {
605
+ if ( ImageAlignment == Alignment . Start || ImageAlignment == Alignment . Left )
606
+ {
607
+ width = ( ImageAlignment == Alignment . Start && _isRightToLeft ) ? Math . Abs ( ( float ) ( width - _textRectF . X - ImageSize - ( _leftIconPadding * 2 ) - _textAreaPadding - ( StrokeThickness / 2 ) - Padding . Right ) )
608
+ : Math . Abs ( ( float ) ( width - _textRectF . X - ( StrokeThickness / 2 ) - _textAreaPadding - Padding . Right ) ) ;
609
+
610
+ }
611
+ else if ( ImageAlignment == Alignment . End || ImageAlignment == Alignment . Right )
612
+ {
613
+ width = ( ImageAlignment == Alignment . End && _isRightToLeft ) ? Math . Abs ( ( float ) ( width - _textRectF . X - ( StrokeThickness / 2 ) - _textAreaPadding - Padding . Right ) )
614
+ : Math . Abs ( ( float ) ( width - _textRectF . X - ImageSize - _leftIconPadding - _leftIconPadding - _textAreaPadding - ( StrokeThickness / 2 ) - Padding . Right ) ) ;
615
+ }
616
+ else if ( ImageAlignment == Alignment . Top )
617
+ {
618
+ width = Math . Abs ( ( float ) ( width - _textAreaPadding - Padding . Left - Padding . Right - _textAreaPadding - ( StrokeThickness / 2 ) - ( StrokeThickness / 2 ) ) ) ;
619
+ }
620
+ else if ( ImageAlignment == Alignment . Bottom )
621
+ {
622
+ width = Math . Abs ( ( float ) ( width - _textAreaPadding - Padding . Left - Padding . Right - _textAreaPadding - ( StrokeThickness / 2 ) - ( StrokeThickness / 2 ) ) ) ;
623
+ }
624
+ else
625
+ {
626
+ width = Math . Abs ( ( float ) ( width - _textRectF . X - ( StrokeThickness / 2 ) - _textAreaPadding - Padding . Right ) ) ;
627
+ }
628
+ }
629
+ else
630
+ {
631
+ width = Math . Abs ( ( float ) ( width - _textAreaPadding - Padding . Left - Padding . Right - _textAreaPadding - StrokeThickness ) ) ;
632
+ }
633
+ return width ;
634
+ }
635
+
590
636
/// <summary>
591
637
/// Measures the size of child elements if present and returns the size.
592
638
/// </summary>
@@ -735,7 +781,10 @@ internal override void DrawText(ICanvas canvas, RectF dirtyRect)
735
781
canvas . SaveState ( ) ;
736
782
float availableWidth = _textRectF . Width ;
737
783
#if ANDROID
738
- availableWidth -= AndroidTextMargin ;
784
+ if ( LineBreakMode != LineBreakMode . WordWrap )
785
+ {
786
+ availableWidth -= AndroidTextMargin ;
787
+ }
739
788
#endif
740
789
var trimmedText = _isFontIconText ? Text : StringExtensions . GetTextBasedOnLineBreakMode ( ApplyTextTransform ( Text ) , this , availableWidth , _textRectF . Height , LineBreakMode ) ;
741
790
if ( _textRectF . Width > 0 && _textRectF . Height > 0 )
0 commit comments