我正在尝试在不同的单词上创建一个具有不同样式的“标签”,就像这里描述的那样。问题是 - 据我所知 - UATextLayer 的 MonoTouch 实现不接受将 NSAttributedString 分配给 String 属性,因为 String 属性具有字符串类型。
这是实施中的错误还是有另一种方法?
(是的,我知道我可以添加单独的标签 - 但如果有更好的解决方案,我宁愿不要)。
编辑(响应米格尔的回答):
在更改为 GetHandler 并更正为“void_objc_msgSend_IntPtr”而不是“void_objc_msgSend_IntPrt”后,答案中的代码编译并运行,但它无论如何都不能正常工作(我将其标记为答案有点快)。不会引发任何错误,但不会显示文本。
代码:
string _text="Example string";
if(_textLayer==null) {
_textLayer = new CATextLayer();
_textLayer.Frame = new RectangleF(50,698,774,50);
_textLayer.Wrapped=true;
_textLayer.ForegroundColor=UIColor.White.CGColor;
_textLayer.BackgroundColor=UIColor.Clear.CGColor;
Layer.AddSublayer(_textLayer);
}
//_textLayer.String=_text;
CTFont _font=new CTFont("MarkerFelt-Thin",48);
CTStringAttributes _attrs=new CTStringAttributes();
_attrs.Font=_font;
_attrs.ForegroundColor = UIColor.White.CGColor;
var nsa = new NSAttributedString(_text);
Messaging.void_objc_msgSend_IntPtr(
_textLayer.Handle,
Selector.GetHandle("string"),
nsa.Handle);
如果我取消注释_textLayer.String=_text
我会看到文本(但当然没有属性),所以问题不在于图层。