Bug 33501 - LabelRenderer sets BackgroundColor on its container instead of the native control
Summary: LabelRenderer sets BackgroundColor on its container instead of the native con...
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.4.4
Hardware: All All
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-08-31 00:44 UTC by david
Modified: 2015-09-02 20:55 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description david 2015-08-31 00:44:53 UTC
LabelRenderer on iOS sets BackgroundColor on the container, not on the UILabel (i.e. the Control property).

This makes it very difficult to successfully subclass the LabelRenderer and add things like rounded borders to it, because the container view has a different shape.

Please make sure the renderer sets properties only on the native control and not on the container UIView.
Comment 1 Jason Smith [MSFT] 2015-08-31 14:36:22 UTC
The reason for this is because the iOS UILabel does not support vertical alignment.

In order to support vertical alignment, we make the Control the minimum viable height at all times, and then align it vertically within the container itself. Unfortunately this means that if the BackgroundColor were set on the Control you would not get the expected result, so by setting it on the container you do.
Comment 2 david 2015-09-01 09:25:56 UTC
I understand, and that looks at face value like an OK solution.

However, as I pointed out, subclassing the resulting control becomes quite tricky. You can't just add rounded corners to the Control property's Layer because they'll be in the wrong place.

So I took an hour or so and made a custom UILabel subclass that _does_ support vertical centring, and used that as the Control property in a renderer instead of the plain vanilla UILabel.

Here it is:

https://gist.github.com/5e356ff31256603f5625.git

Feel free to use and/or adapt that code as required. I'm sure you can combine the ideas in that gist with the rest of the LabelRenderer logic quite easily.