1

使用 monotouch,我尝试在多行上显示核心文本元素。我尝试使用的示例来自CoreText_Programming p17。我遇到的一个问题是无法找到 CFMutableAttributedStringRef 的等效项,因此我尝试将其替换为 NSAttributedString,但是以下代码没有显示任何内容。有谁知道单触式中这种类型的任何例子,或者知道以下不起作用的原因。谢谢。

    public override void Draw (RectangleF rect)
    {
        base.Draw (rect);   

        CGContext context = UIGraphics.GetCurrentContext ();
        context.TextMatrix = new CGAffineTransform();           
        CGPath path = new CGPath();
        path.AddRect ( this.Bounds );
        // created dictionary so this line does not crash 
        NSAttributedString stringToDraw = new NSAttributedString( "Hello", new NSDictionary() );
        CTFramesetter framesetter = new CTFramesetter(stringToDraw);
        CTFrame frame = framesetter.GetFrame( new NSRange(0,0), path, null );
        frame.Draw( context );
    }
4

2 回答 2

2

碰巧的是,我在编写/测试 MonoTouch.CoreText 框架时将大部分示例移植到了 MonoTouch。然后显然完全忘记了将它们合并到上游。:-/

也就是说,看看CoreTextDemo.cs,它移植了(几乎逐行)您链接到的 PDF 中的大多数示例。

根据我的CoreTextDemo.cs,你错过了:

  • 正确的CGAffineTransform.MakeScale()调用:context.TextMatrix = CGAffineTransform.MakeScale(1f, -1f);
  • 你没有使用NSMutableAttributedString. (CFMutableAttributedStringRef映射到NSMutableAttributedString。)
  • 你不会处理你的framesetter,也不是你的frame

可能还有其他代码,但当时CoreTextDemo.cs代码的工作方式等同于 Objective-C 代码。

于 2011-07-27T23:08:33.520 回答
0

不确定这是否正确,但这里......

来自 Apple 的CFMutableAttributedStringRef参考文档:

重要提示:为属性字符串设置的属性字典必须始终使用 kCFCopyStringDictionaryKeyCallbacks 为其字典键回调和 kCFTypeDictionaryValueCallBacks 为其值回调创建;否则这是一个错误。

所以我相信用new NSDictionary()参数创建它是不够的。

于 2011-07-26T11:24:02.840 回答