首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CTFont与CTStringAttributes和NSMutableAttributedString在xamarin上崩溃了我的应用程序

CTFont与CTStringAttributes和NSMutableAttributedString在xamarin上崩溃了我的应用程序
EN

Stack Overflow用户
提问于 2013-08-28 10:26:35
回答 1查看 1.6K关注 0票数 1

我正在尝试创建NSMutableAttributedString并使用SetProperties方法设置它的属性。但我的应用程序出错了,

代码语言:javascript
复制
MonoTouch.Foundation.MonoTouchException exception - NSInvalidArgumentException Reason: unrecognized selector sent to instance 0xc305d00.*

代码:

代码语言:javascript
复制
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;

此代码位于UITableViewControllerUITableViewController方法中。我想要能够改变字体或颜色,只有前3-4个字母的字符串。

我发现,如果我消除了Font组件并设置了另一个属性,例如StrokeWidth,它就可以正常工作

代码语言:javascript
复制
var attrs = new CTStringAttributes { /*Font = font*/ StrokeWidth = 5f };

因此,字体似乎被初始化了,有些不正确。为什么会这样呢?为什么它会毁了我的应用程序?

谢谢你的提前!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-28 13:05:39

下面是一个使用UIStringAttributes的示例:

代码语言:javascript
复制
        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);

这是在UIButton上设置两个不同字体的示例。Theme是我自己的静态类,你可以用自己的颜色和字体来代替。

您应该能够在UIStringAttributesCTStringAttributes中完成相同的任务。您可能会为崩溃的案例向Xamarin发布bug报告:http://bugzilla.xamarin.com

*注意:属性化字符串仅适用于iOS 6及更高版本。这就是苹果公司添加这些API的时候。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18485346

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档