1

NSMutableAttributedStringメソッドを使用してそのプロパティを作成および設定しようとしていますSetProperties。しかし、私のアプリはエラーでクラッシュし、

MonoTouch.Foundation.MonoTouchException exception - NSInvalidArgumentException Reason: unrecognized selector sent to instance 0xc305d00.*

コード:

var richTask = new NSMutableAttributedString ("Random");
var fda = new CTFontDescriptorAttributes {
    FamilyName = "Courier",
    StyleName = "Bold",
    Size = 18f
};
var fd = new CTFontDescriptor (fda);
var font = new CTFont (fd, 0);

var attrs = new CTStringAttributes { Font = font };
var range = new NSRange (0, 3);
richTask.SetAttributes(attrs, range);

_label.AttributedText = richTask;

このコードは のGetCellメソッドにありUITableViewControllerます。文字列の最初の 3 ~ 4 文字のみのフォントまたは色を変更できるようにしたい。

Fontコンポーネントを削除して、 StrokeWidthなどの別のプロパティを設定すると、正常に動作することがわかりました

var attrs = new CTStringAttributes { /*Font = font*/ StrokeWidth = 5f };

そのため、フォントの初期化が多少間違っているようです。何故ですか?アプリがクラッシュするのはなぜですか?

前もってありがとう!

4

1 に答える 1

3

UIStringAttributes代わりに使用する例を次に示します。

        string line1 = "Don't have Facebook?", line2 = "\nCreate or sign in with an alternate account";
        var attributedString = new NSMutableAttributedString(line1 + line2);

        attributedString.SetAttributes(new UIStringAttributes
        {
            Font = Theme.BoldSmallFont,
            ForegroundColor = Theme.LightGray,

        }.Dictionary, new NSRange(0, line1.Length));

        attributedString.SetAttributes(new UIStringAttributes
        {
            Font = Theme.RegularSmallFont,
            ForegroundColor = Theme.LightGray,

        }.Dictionary, new NSRange(line1.Length, line2.Length));

        _alternateSignIn.SetAttributedTitle(attributedString, UIControlState.Normal);

これは、 に 2 つの異なるフォントを設定する例ですUIButtonThemeは私自身の静的クラスです。色とフォントを独自のものに置き換えることができます。

UIStringAttributesと同じことを達成できるはずですCTStringAttributes。クラッシュした場合のバグ レポートを Xamarin に投稿できます: http://bugzilla.xamarin.com

*注: 属性付き文字列は、iOS 6 以降でのみ機能します。これは、Apple がこれらの API を追加したときです。

于 2013-08-28T13:05:39.690 に答える