Emanuele Sabetta
Emanuele Sabetta

Reputation: 1611

How to use CoreText in MonoTouch to write rotated text?

I need to write some text rotated by 90 degrees. I first tried with a UILabel rotated with CGAffineTransform:

this.myLabel.Center = this.myLabel.Frame.Location;
this.myLabel.Transform = CGAffineTransform.MakeRotation (-(float)Math.PI / 2.0f);

It rotated it just right, but fonts now look all blurred and difficult to read.

Is there a way to use the CoreText library to make a simple text rotation in a UILabel that doesn't look blurred?

Thank you in advance!

Upvotes: 2

Views: 1561

Answers (2)

poupou
poupou

Reputation: 43553

In case you missed it Xamarin provides a sample on github that use CoreText to draw (non-rotated) text.

Since the text ends up as a path it should not be hard to rotate the whole text. OTOH CoreText is more much complicated than the older API - that's the price to pay to get (way) more control.

Upvotes: 2

cocoahero
cocoahero

Reputation: 1302

You could use Core Graphics. It's a little involved, but will do the trick.

A sample would be:

void MyDrawText (CGContextRef myContext, CGRect contextRect) // 1
{
float w, h;
w = contextRect.size.width;
h = contextRect.size.height;

CGAffineTransform myTextTransform; // 2
CGContextSelectFont (myContext, // 3
                "Helvetica-Bold",
                 h/10,
                 kCGEncodingMacRoman);
CGContextSetCharacterSpacing (myContext, 10); // 4
CGContextSetTextDrawingMode (myContext, kCGTextFillStroke); // 5

CGContextSetRGBFillColor (myContext, 0, 1, 0, .5); // 6
CGContextSetRGBStrokeColor (myContext, 0, 0, 1, 1); // 7
myTextTransform =  CGAffineTransformMakeRotation  (myRadianRotation); // 8
CGContextSetTextMatrix (myContext, myTextTransform); // 9
CGContextShowTextAtPoint (myContext, 40, 0, "Quartz 2D", 9); // 10
}

Upvotes: 1

Related Questions