Bug 58111 - UILabelAppearance does not expose the Line property
Summary: UILabelAppearance does not expose the Line property
Alias: None
Product: iOS
Classification: Xamarin
Component: BCL Class Libraries ()
Version: XI 10.10 (d15-2)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2017-07-12 15:47 UTC by Andres
Modified: 2017-07-19 12:13 UTC (History)
6 users (show)

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:

Description Andres 2017-07-12 15:47:36 UTC
In iOS Xcode it is possible to set the numberOfLines of all the UILabel instances by using the appearance, this is possible by doing this:

    UILabel.appearance().numberOfLines = 2

In Visual Studio (both Mac and Windows) I try

    UILabel.Appearance.Lines = 2;

But receive a message saying:

 'UILabel.UILabelAppearance' does not contain a definition for 'Lines' and no extension method 'Lines' accepting a first argument of type 'UILabel.UILabelAppearance' could be found (are you missing a using directive or an assembly reference?)

Navigating to the implementation of the UILabelAppearance I can see that the Lines property is not listed only these: 

UIFont Font
UIColor HighlightedTextColor 
UIColor ShadowColor 
CGSize ShadowOffSet 
UIColor TextColor

 ==== Xamarin Information ====

Visual Studio Community 2017 for Mac
Version 7.0.1 (build 24)
Installation UUID: 1efa48e6-2eb5-4248-823d-5664a059f0d7
	Mono (2017-02/5077205) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 500010001


.NET Core
Runtime: Not installed
SDK: Not installed
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.0.1/lib/mono/msbuild/15.0/bin/Sdks

Version: 1.5.4
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

Version: (Visual Studio Community)
Android SDK: /Users/APinedaMartinez/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		6.0 (API level 23)

SDK Tools Version: 25.2.5
SDK Platform Tools Version: 25.0.5
SDK Build Tools Version: 25.0.3

Java SDK: /usr
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

Android Designer EPL code available here:

Apple Developer Tools
Xcode 8.3 (12169)
Build 8E3004b

Version: (Visual Studio Community)
Hash: d2270eec
Branch: d15-2
Build date: 2017-05-22 16:30:53-0400

Xamarin Inspector
Version: 1.2.2
Hash: b71b035
Branch: d15-1
Build date: Fri, 21 Apr 2017 17:57:12 GMT

Version: (Visual Studio Community)

Build Information
Release ID: 700010024
Git revision: 7ab1ca2ced6f584e56b7a0d4d321d00775cd95c9
Build date: 2017-05-19 05:44:51-04
Xamarin addins: 08d17158f3365beee5e60f67999e607cce4b3f93
Build lane: monodevelop-lion-d15-2

Operating System
Mac OS X 10.12.5
Darwin 16.6.0 Darwin Kernel Version 16.6.0
    Fri Apr 14 16:21:16 PDT 2017
    root:xnu-3789.60.24~6/RELEASE_X86_64 x86_64
Comment 1 Sebastien Pouliot 2017-07-13 01:16:58 UTC
This is because this selector is not decorated with an `UI_APPEARANCE_SELECTOR` macro, e.g. in UILabel.h 

> @property(nonatomic) NSInteger numberOfLines;

unlike other properties in other types, e.g.

> @property(nonatomic) UIBarStyle barStyle UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED; // default is UIBarStyleDefault

That means the selector is not supported for UIAppearance by Apple - but it will compile and _might_ work [1].

Why ? 

(a) Apple current appearance implementation (which might change and we've been told not to depend on) is a simple (currently unvalidated) proxy [2] to an instance of the type. So every selector can be used, even if they are not compatible (or related) with UIAppearance.

(b) ObjC is not type safe and the use of a macro (not types) hides this further. This means it will compile in ObjC just fine. Whether it will work (or not) varies per selector and iOS versions.

To avoid bad surprises the managed API provided in Xamarin.iOS.dll only expose the officially supported UIAppearance selectors (i.e. it use a custom type to enforce type safety).

Still want it ? Here's a workaround, subverting C# type safety, that does the same as the ObjC code you posted:

> var a = Runtime.GetNSObject<UILabel> (UILabel.Appearance.Handle);
> a.Lines = 2;

This treats the `UILabel.Appearance` (singleton) just like it was a normal `UILabel` (exactly like the ObjC code you posted) and match the current Apple implementation. As long as the ObjC code works then the C# version will behave identically. 

[1] YMMV between selectors and iOS versions https://stackoverflow.com/a/11817191/220643 https://stackoverflow.com/a/11839198/220643

[2] https://developer.apple.com/documentation/uikit/ui_appearance_selector
Comment 2 Andres 2017-07-19 00:39:32 UTC
Thanks for the information, did not know this.
Comment 3 ugur.pelister 2017-07-19 12:13:35 UTC
Hi. you stated that Ocj-c is not a type-safe language, hence the compilation with the "numberOfLines" property succeeds. but swift, which is a strongly typed language, does support the same property on UILabelAppearance.