Bug 15402 - lineBreakMode: unrecognised selector exception when setting ParagraphStyle with CTParagraphStyle
Summary: lineBreakMode: unrecognised selector exception when setting ParagraphStyle wi...
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 7.0.0.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Sebastien Pouliot
Depends on:
Reported: 2013-10-15 09:41 UTC by zldrq
Modified: 2013-10-17 14:32 UTC (History)
2 users (show)

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

test project that throws exception (13.17 KB, application/zip)
2013-10-15 18:36 UTC, zldrq

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 zldrq 2013-10-15 09:41:26 UTC
somewhere after running this code i get exception
        Objective-C exception thrown.  Name: NSInvalidArgumentException Reason: -[__NSCFType lineBreakMode]: unrecognized selector sent to instance 0xe5a4db0

the code is as follows

        var attr = new CTStringAttributes ();
        attr.Font = new CTFont("Parangon110C", 11);

        var paragraph = new CTParagraphStyleSettings ();
        paragraph.Alignment = CTTextAlignment.Justified;
        attr.ParagraphStyle = new CTParagraphStyle(paragraph);

        DescLabel.AttributedText = new NSAttributedString(billboard.Desc, attr);

i've figured out that it only appears when i set up the attr.ParagraphStyle. but when it is null, everything is ok (except i can't get text formatted my way lol). after executing a string 
DescLabel.AttributedText = new NSAttributedString(billboard.Desc, attr);
debugger shows the exception text in Size property of DescLabel.AttributedText

when i'm trying to go around with UIStringAttributes it throws nonimplemented exception when setting UIParagraphStyle attributes
Comment 1 Sebastien Pouliot 2013-10-15 14:22:30 UTC
I copy paste this into a unit test and did not get that exception (from ObjC).

What version of the iOS simulator (and/or devices) are you using ?

Just to be 100% safe also include all software versions you're using. The easiest way to get exact version information is to use the "Xamarin Studio" menu, "About Xamarin Studio" item, "Show Details" button and copy/paste the version informations (you can use the "Copy Information" button).
Comment 2 zldrq 2013-10-15 14:34:29 UTC
are you using it to display actual ULabel?
exception shows up at string UIApplication.Main (args, null, "AppDelegate") in main.cs

Xamarin Studio
Version 4.0.12 (build 3)

Apple Developer Tools
Xcode 5.0 (3332.25)
Build 5A1413


Operating System
Mac OS X 10.8.5
Comment 3 zldrq 2013-10-15 14:54:02 UTC
also i created a new project and tried this with no result. also i switched deployment targets and nothing changes.
i've heard that this attributes were not available prior to ios 6, but i have ios7  sdk ftw
Comment 4 Sebastien Pouliot 2013-10-15 17:40:20 UTC
> What version of the iOS simulator (and/or devices) are you using ?

You have not answered my previous question. The SDK can be 7.0 but if you're using an older iOS version (either in the simulator or a device) then this will fail.

> are you using it to display actual ULabel?

It was assigned to a UILabel, but not displayed - that might a difference.

> also i created a new project and tried this with no result.

Please attach the test project. That'll make sure we're testing the exact same code along with the same build options.
Comment 5 zldrq 2013-10-15 18:31:24 UTC
sorry, didn't pay attention, i was thinking it's little obvious. ios version is 7

i think yes, it might be a difference. i suspect exception thrown while system trying to show the very label.

i'm attaching the project that i was testing on
Comment 6 zldrq 2013-10-15 18:36:07 UTC
Created attachment 5148 [details]
test project that throws exception
Comment 7 zldrq 2013-10-15 18:44:04 UTC
i've found this so called 'bug report'
the problem is looks like mine, but resolved by just correcting obvious mistake. i think it's resolved, because no further comments follow

based on that, i was thinking may be ParagraphStyle style object is somehow mistakenly destroyed by trash collector or something.. but no, exception text in Size property of NSAttributedText appears where ParagraphStyle is surely alive
Comment 8 Sebastien Pouliot 2013-10-15 20:31:29 UTC
Thanks. I can duplicate with the test case and the native stack trace should help (it shows this happens later, not when the attributes are created).

Native stacktrace:

mono-rt: 	0   test3                               0x000b50fd mono_handle_native_sigsegv + 349

mono-rt: 	1   test3                               0x000c00ca sigabrt_signal_handler + 122

mono-rt: 	2   libsystem_platform.dylib            0x053fcdeb _sigtramp + 43

mono-rt: 	3   ???                                 0xffffffff 0x0 + 4294967295

mono-rt: 	4   libsystem_sim_c.dylib               0x05119e12 abort + 127

mono-rt: 	5   test3                               0x00230fd3 monotouch_unhandled_exception_handler + 291

mono-rt: 	6   test3                               0x000b592b mono_invoke_unhandled_exception_hook + 91

mono-rt: 	7   test3                               0x000b4949 mono_handle_exception_internal + 3817

mono-rt: 	8   test3                               0x000b3a59 mono_handle_exception + 41

mono-rt: 	9   test3                               0x000591a9 mono_x86_throw_exception + 281

mono-rt: 	10  ???                                 0x0e6bfe67 0x0 + 241958503

mono-rt: 	11  test3                               0x00222df1 monotouch_exception_handler + 177

mono-rt: 	12  CoreFoundation                      0x0168ca1d __handleUncaughtException + 749

mono-rt: 	13  libobjc.A.dylib                     0x04e5eb5c _ZL15_objc_terminatev + 100

mono-rt: 	14  libc++abi.dylib                     0x05332f60 _ZSt11__terminatePFvvE + 14

mono-rt: 	15  libc++abi.dylib                     0x0533297d _ZN10__cxxabiv1L22exception_cleanup_funcE19_Unwind_Reason_CodeP17_Unwind_Exception + 0

mono-rt: 	16  libobjc.A.dylib                     0x04e5e9cd _ZL26_objc_exception_destructorPv + 0

mono-rt: 	17  CoreFoundation                      0x01699903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275

mono-rt: 	18  CoreFoundation                      0x015ec90b ___forwarding___ + 1019

mono-rt: 	19  CoreFoundation                      0x015ec4ee _CF_forwarding_prep_0 + 14

mono-rt: 	20  UIFoundation                        0x08147fa6 __NSStringDrawingEngine + 494

mono-rt: 	21  UIFoundation                        0x0814f3ab -[NSAttributedString(NSExtendedStringDrawing) boundingRectWithSize:options:context:] + 1290

mono-rt: 	22  UIKit                               0x02ae3c9a -[UILabel _drawTextInRect:baselineCalculationOnly:] + 758

mono-rt: 	23  UIKit                               0x02ae30e4 -[UILabel drawTextInRect:] + 577

mono-rt: 	24  UIKit                               0x02ae505e -[UILabel drawRect:] + 98

mono-rt: 	25  UIKit                               0x0299d8c6 -[UIView(CALayerDelegate) drawLayer:inContext:] + 504

mono-rt: 	26  QuartzCore                          0x05ed4209 -[CALayer drawInContext:] + 123

mono-rt: 	27  QuartzCore                          0x05ed413a _ZL16backing_callbackP9CGContextPv + 96

mono-rt: 	28  QuartzCore                          0x05dc5364 CABackingStoreUpdate_ + 2656

mono-rt: 	29  QuartzCore                          0x05ed40d2 ___ZN2CA5Layer8display_Ev_block_invoke + 93

mono-rt: 	30  QuartzCore                          0x05f07f63 x_blame_allocations + 15

mono-rt: 	31  QuartzCore                          0x05ed3f3d _ZN2CA5Layer8display_Ev + 1519

mono-rt: 	32  QuartzCore                          0x05ed4189 -[CALayer _display] + 33

mono-rt: 	33  QuartzCore                          0x05ed3946 _ZN2CA5Layer7displayEv + 144

mono-rt: 	34  QuartzCore                          0x05ed4163 -[CALayer display] + 33

mono-rt: 	35  QuartzCore                          0x05ec8313 _ZN2CA5Layer17display_if_neededEPNS_11TransactionE + 323

mono-rt: 	36  QuartzCore                          0x05ec838c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 38

mono-rt: 	37  QuartzCore                          0x05e30156 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294

mono-rt: 	38  QuartzCore                          0x05e314e1 _ZN2CA11Transaction6commitEv + 393

mono-rt: 	39  QuartzCore                          0x05eed870 +[CATransaction flush] + 52

mono-rt: 	40  UIKit                               0x02931975 -[UIApplication _reportAppLaunchFinished] + 39

mono-rt: 	41  UIKit                               0x02932554 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 2260

mono-rt: 	42  UIKit                               0x0294642c -[UIApplication handleEvent:withNewEvent:] + 3447

mono-rt: 	43  UIKit                               0x02946999 -[UIApplication sendEvent:] + 85

mono-rt: 	44  UIKit                               0x02933c35 _UIApplicationHandleEvent + 736

mono-rt: 	45  GraphicsServices                    0x0606d2eb _PurpleEventCallback + 776

mono-rt: 	46  GraphicsServices                    0x0606cdf6 PurpleEventCallback + 46

mono-rt: 	47  CoreFoundation                      0x01577dd5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53

mono-rt: 	48  CoreFoundation                      0x01577b0b __CFRunLoopDoSource1 + 523

mono-rt: 	49  CoreFoundation                      0x015a27ec __CFRunLoopRun + 2156

mono-rt: 	50  CoreFoundation                      0x015a1b33 CFRunLoopRunSpecific + 467

mono-rt: 	51  CoreFoundation                      0x015a194b CFRunLoopRunInMode + 123

mono-rt: 	52  UIKit                               0x029316ed -[UIApplication _run] + 840

mono-rt: 	53  UIKit                               0x0293394b UIApplicationMain + 1225

mono-rt: 	54  ???                                 0x13175bfa 0x0 + 320297978

mono-rt: 	55  ???                                 0x13173d54 0x0 + 320290132

mono-rt: 	56  ???                                 0x13173930 0x0 + 320289072

mono-rt: 	57  ???                                 0x13173a6f 0x0 + 320289391

mono-rt: 	58  test3                               0x000e4a26 mono_jit_runtime_invoke + 790

mono-rt: 	59  test3                               0x0017ee3f mono_runtime_invoke + 127

mono-rt: 	60  test3                               0x001846b3 mono_runtime_exec_main + 435

mono-rt: 	61  test3                               0x00184454 mono_runtime_run_main + 628

mono-rt: 	62  test3                               0x0005140d mono_jit_exec + 93

mono-rt: 	63  test3                               0x0021c17a main + 2426

mono-rt: 	64  test3                               0x00003675 start + 53
Comment 9 Sebastien Pouliot 2013-10-15 21:27:42 UTC
There were two invalid values (bug*) that ends up in the internal dictionary built for `CTParagraphStyleSettings`. 

* Setting `Alignment` cause this value to be copied to other properties.

note: fixed in maccore 00bde33919de29d9ac19d07727578e08366526d7

However that's not a complete fix (not closing the bug) and something else is off.

In the mean time if you're targeting iOS 6.0+ then a workaround is to use the newer `UIStringAttributes` like this (the managed API is quite similar):

	var attr = new UIStringAttributes ();
	attr.Font = UIFont.FromName ("Parangon110C", 11);
	attr.ParagraphStyle = new NSMutableParagraphStyle () {
		Alignment = UITextAlignment.Justified
	DescLabel.AttributedText = new NSAttributedString("you'd got to see an exception instead of it if you were me!", attr);

Hopefully that will unblock you until a complete fix is found and available.
Comment 10 zldrq 2013-10-16 07:29:01 UTC
thanks, you saved me!
but for some reason text can't be justified. it can be aligned right or left or center, but not justified..
may be you know something about this too?
Comment 11 Sebastien Pouliot 2013-10-16 10:34:54 UTC
That's likely because it's an UILabel and that type suffers from limitations wrt attributed strings, in particular with paragraph styles [1]. Even specifying a range for the attribute does not work when I tried it.

Many people try this approach and go back to basic UILabel API [2]. That might be an easy workaround if you do not need advanced control of the attributes, e.g.

			DescLabel.TextAlignment = UITextAlignment.Left;
			DescLabel.Lines = 0;

Sadly UILabel never supported Justified :(

As an alternative you might want to use a more elaborate control (e.g. UITextView) to emulate a more advanced UILabel. There's a TextKit demo [3] that could prove useful.

The last, most complex. option is to use CoreText to draw (to the View) the text exactly like you want it.

[1] http://www.cocoabuilder.com/archive/cocoa/323722-nsattributedstring-mysteriously-truncated-too-soon.html#323732

[2] http://stackoverflow.com/questions/13206657/new-nsattributedstring-multiline

[3] http://docs.xamarin.com/samples/TextKitDemo
Comment 12 Sebastien Pouliot 2013-10-16 10:40:29 UTC
Using the same `attr` from above and a `UITextView` named `tv` you can do:

	tv.AttributedText = new NSAttributedString ("you'd got to see an exception instead of it if you were me!", attr);
	tv.Editable = false;
	tv.Selectable = false;

to get a very-much alike, justified, label.
Comment 13 zldrq 2013-10-16 11:04:47 UTC
thank you!
i wonder why i used label in place where obviously must be text view
coding too much perhaps..
Comment 14 Sebastien Pouliot 2013-10-17 14:32:40 UTC
-[__NSCFType lineBreakMode]: unrecognized selector sent to instance 0xe1e1260

^ the instance handle points to the CTParagraphStyle instance being created. That's weird since this is a CoreText API (which is not ObjC based). OTOH it's not very surprising since commenting the related line makes the sample "works" (as in "no ObjC exception").

NSParagraphStyle is very much alike CTParagraphStyle (and is an ObjC API) but nowhere it is documented to be toll free bridge with CTParagraphStyle. However there are hints [1] that Apple use them interchangeably since iOS6.

Now since the same issue happens in ObjC applications [2] I'm pretty sure this is an Apple bug (and not something we can fix inside XI)

[1] http://openradar.appspot.com/12354615
[2] http://stackoverflow.com/q/14507990/220643