开发者

Core Text FrameSetter with monotouch

Using monotouch, I'm try to display a core text element over multiple lines. The example I'm t开发者_StackOverflow中文版rying to use is from CoreText_Programming p17. One problem I've come across is not being able to find an equivalent for CFMutableAttributedStringRef, so I've tried to subsitute this for NSAttributedString, however the following code displays nothing. Does anyone know of any examples of this type in monotouch or know the reason why the following is not working. Thanks.

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


As it happens, I ported most of those samples to MonoTouch when I was writing/testing the MonoTouch.CoreText framework. Then apparently completely forgot to merge them upstream. :-/

That said, take a look at CoreTextDemo.cs, which ports (nearly line-for-line) most of the samples in the PDF you linked to.

Based on my CoreTextDemo.cs, you're missing:

  • A proper CGAffineTransform.MakeScale() call: context.TextMatrix = CGAffineTransform.MakeScale(1f, -1f);
  • You're not using NSMutableAttributedString. (CFMutableAttributedStringRef maps to NSMutableAttributedString.)
  • You're not disposing of your framesetter, nor your frame.

There may be others, but at the time the CoreTextDemo.cs code worked equivalent to the Objective-C code.


Not sure if this is correct but here goes...

From Apple's CFMutableAttributedStringRef Reference doc:

Important: Attribute dictionaries set for an attributed string must always be created with kCFCopyStringDictionaryKeyCallbacks for their dictionary key callbacks and kCFTypeDictionaryValueCallBacks for their value callbacks; otherwise it's an error.

So I believe creating it with the new NSDictionary() parameter is not enough.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜