Skip to content

Button does not resize to fit text with CharacterSpacing #10293

Closed

Description

@tdeborde2

Description

Button is not sized to account for CharacterSpacing on iOS and MacCatalyst.

iOS and MacCatalyst
Screen Shot 2022-09-23 at 11 06 55 AM

Android and Windows
Screen Shot 2022-09-23 at 12 40 41 PM

Steps to Reproduce

  1. Create a new .NET MAUI app
  2. Modify the button on MainPage.xaml to include CharacterSpacing="1"
  3. Run the application on iOS or MacCatalyst

Expected:
Screen Shot 2022-09-23 at 12 40 41 PM

Actual:
Screen Shot 2022-09-23 at 11 06 55 AM

Link to public reproduction project repository

https://github.com/tdeborde2/MauiCharacterSpacing

Version with bug

6.0.486 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS, macOS

Affected platform versions

iOS 15.5, MacOS 12.6

Did you find any workaround?

I was able to workaround the issue with a custom button control.
This can be found in the repro project.

public class CustomButton : Button
{
	bool _sizeAllocated;

	double _oldCharacterSpacing;

	protected override void OnSizeAllocated(double width, double height)
	{
		if(!_sizeAllocated && WidthRequest == -1 && HorizontalOptions.Alignment != LayoutAlignment.Fill)
		{
			_sizeAllocated = true;
			this.WidthRequest = width + ((Text.Length - 1) * CharacterSpacing);
			return;
		}
		base.OnSizeAllocated(width, height);
	}

	protected override void OnPropertyChanging([CallerMemberName] string propertyName = null)
	{
		base.OnPropertyChanging(propertyName);
		if(propertyName == "Text")
		{
			if (_sizeAllocated)
			{
				WidthRequest = -1;
				_sizeAllocated = false;
			}
		}
	}

	protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
	{
		base.OnPropertyChanged(propertyName);
		if (_sizeAllocated && HorizontalOptions.Alignment != LayoutAlignment.Fill)
		{
			if (propertyName == "CharacterSpacing")
			{
				if (CharacterSpacing < _oldCharacterSpacing)
				{
					WidthRequest = Width - ((Text.Length - 1) * (_oldCharacterSpacing - CharacterSpacing));
				}
				if (CharacterSpacing > _oldCharacterSpacing)
				{
					WidthRequest = Width + ((Text.Length - 1) * (CharacterSpacing - _oldCharacterSpacing));
				}
			}
		}
		_oldCharacterSpacing = CharacterSpacing;
	}
}

Relevant log output

No response

Activity

  1. added
    t/bug
    on Sep 23, 2022
  2. added
    legacy-area-controls
    on Sep 23, 2022
  3. added
    area-layout
    area-controls-button
    on Sep 26, 2022
  4. added this to the Backlog milestone on Sep 29, 2022
  5. hartez commented on Feb 3, 2023

    @hartez
    Contributor

    I think we're handling attributed text in the UIButtons incorrectly; instead of grabbing the internal UILabel and updating it, we should probably be using SetAttributedTitle(). I'd bet if we used that method the UIButtons would update their layouts appropriately.

  6. self-assigned this
    on Feb 9, 2023
  7. moved this from Todo to In Progress in MAUI SDK Ongoingon Feb 10, 2023
  8. moved this from In Progress to Done in MAUI SDK Ongoingon Feb 15, 2023
  9. added a commit that references this issue on Feb 15, 2023
    5cbb27e
  10. added a commit that references this issue on Feb 18, 2023
    445f9a9
  11. added a commit that references this issue on Feb 21, 2023
    0755e9c
  12. added
    fixed-in-8.0.0-preview.1.7762
    on Feb 22, 2023
  13. modified the milestones: Backlog, 8.0-preview1 on Feb 22, 2023
  14. ghost locked as resolved and limited conversation to collaborators on Mar 24, 2023
  15. removed
    legacy-area-controls
    on May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-controls-buttonarea-layoutfixed-in-8.0.0-preview.1.7762platform/iost/bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions