-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
iOS: button won't expand vertically to fit long text when LineBreakMode="WordWrap" #22054
Comments
Can repro this issue at iOS platform on the latest 17.10 Preview 5 (8.0.6&8.0.21). |
@Silverado-by I actually don't think this worked in Xamarin.Forms. Or maybe it did in versions near the end, but was broken before that and I never knew to remove the workaround I had in place. Unless this is somehow a different issue than what I encountered in Xamarin. I had to create a You might notice the line The same might be true for the last 3 lines setting protected override void ConnectHandler(UIButton platformView)
{
if (VirtualView is Button button)
{
bool resized = false;
platformView.TitleLabel.Text = platformView.TitleLabel.Text ?? "";
button.SizeChanged += (s, e) =>
{
if (resized == false)
{
CGRect unconstrainedBounds = ((NSString)platformView.TitleLabel.Text).GetBoundingRect(new CGSize(double.MaxValue, double.MaxValue), NSStringDrawingOptions.UsesLineFragmentOrigin, new UIStringAttributes { Font = platformView.TitleLabel.Font }, null);
CGRect constrainedBounds = ((NSString)platformView.TitleLabel.Text).GetBoundingRect(new CGSize(button.Width - button.Padding.HorizontalThickness, double.MaxValue), NSStringDrawingOptions.UsesLineFragmentOrigin, new UIStringAttributes { Font = platformView.TitleLabel.Font }, null);
double difference = button.Height - unconstrainedBounds.Height;
button.HeightRequest = Math.Max(button.Height, constrainedBounds.Height + difference);
resized = true;
}
};
platformView.TitleLabel.LineBreakMode = UILineBreakMode.WordWrap;
platformView.TitleLabel.Lines = 0;
platformView.TitleLabel.TextAlignment = UITextAlignment.Center;
}
base.ConnectHandler(platformView);
} EDIT: Actually, I just checked, and it doesn't look like this code actually works in MAUI at all. So I'll need to figure that out. But maybe this code can put people in the right direction. I'll update if I figure out a workaround. |
After looking into this more and trying to figure out a work around, I think the issue is that in This approach seems wrong on possibly two accounts:
Number 2 seems like the correct approach, along with possible calling Because a lot of this is done with I have something that somewhat works that I could make due with, but it doesn't take into account a button image or other things like a true workaround would. The code below should somewhat solve the problem, but is not ideal: ButtonHandler.Mapper.AppendToMapping(nameof(Button.Text), (h, b) =>
{
try
{
if (b is Button button)
{
h.PlatformView.LayoutSubviews();
double height = h.PlatformView.TitleLabel.Bounds.Height + button.Padding.VerticalThickness;
if (button.MinimumHeightRequest < height)
{
button.MinimumHeightRequest = height;
}
}
}
catch (Exception ex)
{
}
}); |
I don't think this solution is working for me since |
@Silverado-by With this exact code? Make sure you are doing the I did notice that they changed the involved code some between 8.0.7 and 8.0.21, so if you are on 8.0.21 now then the code above may no longer work, though I can't immediately see why. I am working on a better workaround that hopefully I will be able to post shortly, but it hasn't been easy due to a lot of the API being This might end up requiring a derived Ultimately the problem seems to be that none of the sizing/layout methods for I'll try to post something more useful soon. EDIT: Actually, I noticed why this might not be working. Try changing An issue I have run into that makes this even trickier is that the |
Thank you! |
@Silverado-by No problem. I think I have a better/more complete workaround, although I haven't tested it fully. But so you don't have to wait, I'll post it here. Override public override Size GetDesiredSize(double widthConstraint, double heightConstraint)
{
Size desiredSize = base.GetDesiredSize(widthConstraint, heightConstraint);
if (VirtualView is Button button && button.HeightRequest == -1 && (button.LineBreakMode == LineBreakMode.WordWrap || button.LineBreakMode == LineBreakMode.CharacterWrap))
{
double width = widthConstraint - button.Padding.HorizontalThickness;
CGSize size = new(width, heightConstraint);
NSAttributedString title = PlatformView.CurrentAttributedTitle ?? new NSAttributedString(PlatformView.CurrentTitle, new UIStringAttributes()
{
Font = PlatformView.TitleLabel.Font
});
CGRect bounds = title.GetBoundingRect(size, NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading, null);
double height = bounds.Height;
desiredSize.Height = height + button.Padding.VerticalThickness;
}
return desiredSize;
} This is as close as I could get. It works fairly well with
I haven't tested font changes either, but I think that should work. I haven't figured out a way to do it with handler mapping because there doesn't seem to be any way to set/override the It also doesn't work with an button image, but as far as I can tell images and text in a button doesn't really work anyway. Frankly, the entire ContentLayout concept of the buttons seems broken, at least as far as being customizable. I tried to get an image to work, but I gave up since I don't really have a need for that right now anyway. |
Any update on this ticket ? |
@nautilux2 are you using Maui 9? In the release notes, it mentions something about iOS buttons working better at honoring its contents. |
@bzd3y no, I'm using 8 |
Description
Button height behaves differently in Android and iOS when there is long text inside that doesn't fit button properly and LineBreakMode is set to "WordWrap". In Android, it expands vertically to fit all text while in iOS vertical size stays the same while text spreads outside of the button
![button](https://private-user-images.githubusercontent.com/18289449/325605557-1af5aef5-09a2-41fb-9e81-797583e28a55.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkzNDk4NDQsIm5iZiI6MTczOTM0OTU0NCwicGF0aCI6Ii8xODI4OTQ0OS8zMjU2MDU1NTctMWFmNWFlZjUtMDlhMi00MWZiLTllODEtNzk3NTgzZTI4YTU1LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjEyVDA4MzkwNFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWIxMjEyM2Y3ODFkMDEzNDgxMWM3OGMyNDhkOWY5YmE1ZDM5ZmRmM2RkZjc3YzdjZWVhZGM2MzAxMDgyMzc5MGMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.tQNGuekDTwNyOXRFneE9425JIiebKlG_sV1LeTRVw7M)
Steps to Reproduce
Run application from the linked repository in Android and iOS
Link to public reproduction project repository
https://github.com/Silverado-by/ButtonTest
Version with bug
8.0.7 SR2
Is this a regression from previous behavior?
Yes, this used to work in Xamarin.Forms
Last version that worked well
Unknown/Other
Affected platforms
iOS
Affected platform versions
iOS 17.2
Did you find any workaround?
No, but I'd like to know if there is any
Relevant log output
No response
The text was updated successfully, but these errors were encountered: