jungle_mole
jungle_mole

Reputation: 320

lineBreakMode: unrecognised selector exception in MonoTouch when setting ParagraphStyle

subject says it all, so i will comment after showing a code:

        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);

so after running this code i get an exception

Objective-C exception thrown.  Name: NSInvalidArgumentException Reason: -[__NSCFType lineBreakMode]: unrecognized selector sent to instance 0xe5a4db0

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.

i suspect, this is a bug in CTStringAttributes or CTParagraphStyle. i've found there CoreText repository but can't say what's wrong till now

also, little later, i've found a bugreport where someone complaints his stuff not working. but after correcting an obvious error in code (lucky guy), he's not commenting anymore, so the conclusion is: he'd got this working. but his code is similar to mine..

please help me to resolve this or to find workaround. (UIParagraphStyle is not working either, it says that UIParagraphStyleSettings.LineBreakMode setter is not implemented)

Upvotes: 1

Views: 478

Answers (1)

svn
svn

Reputation: 1408

Apparently from IOS 6 and upwards you have to use UIKits NSParagraphStyle for this instead of the CoreText CTParagraphStyle

var parstyle = new MonoTouch.UIKit.NSMutableParagraphStyle ();
parstyle.Alignment = MonoTouch.UIKit.UITextAlignment.Justified;
var att = new NSMutableAttributedString (billboard.Desc);
att.AddAttribute (
    MonoTouch.UIKit.UIStringAttributeKey.ParagraphStyle,
    parstyle,
    new NSRange (0, att.Length)
);
att.AddAttribute (
    MonoTouch.UIKit.UIStringAttributeKey.Font,
    MonoTouch.UIKit.UIFont.FromName ("Parangon110C", 11),
    new NSRange (0, att.Length)
    );
DescLabel.AttributedText = att;

there is also a convenient constructor for NSAttributedString to do this:

var parstyle = new MonoTouch.UIKit.NSMutableParagraphStyle ();
parstyle.Alignment = MonoTouch.UIKit.UITextAlignment.Justified;

DescLabel.AttributedText = new NSAttributedString (
                str: billboard.Desc,
                font: MonoTouch.UIKit.UIFont.FromName ("Parangon110C", 11),
                paragraphStyle: parstyle);

Upvotes: 3

Related Questions