0

I want to draw an NSAttributedString in a rect, just like I would do with a normal string: [normalString drawInRect:rect]; but I can't figure out how. I don't want any UILabel subclasses since I'm implementing a fast scrolling UITableView with no views, so it needs to be drawn. The documentation shows that drawing is only available on Mac.

Is drawing an NSAttributedString not possible on iOS?

Snowman
  • 31,411
  • 46
  • 180
  • 303
  • Is [`CATextLayer`](https://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CATextLayer_class/Introduction/Introduction.html) not adequate? – Aidan Steele Apr 30 '12 at 00:30
  • Wouldn't this involve adding more views? I'd have to add the layer as a sublayer right? – Snowman Apr 30 '12 at 00:35
  • @SedateAlien: `CATextLayer` is good, and may be suitable for this particular tast. However, it doesn't obey all the formatting that can be contained in `NSAttributedString`. See [CATextLayer + NSAttributtedString + CTParagraphStyleRef](http://stackoverflow.com/q/10071198/1318452). – Cowirrie Apr 30 '12 at 10:34

1 Answers1

2

You have to use Core Text to draw attributed strings on iOS.

The basic procedure would be to create a CTFramesetter from the string and a rectangular CGPath (or any other shape you may want), then create the frame using CTFramesetterCreateFrame and draw it with CTFrameDraw.

omz
  • 53,243
  • 5
  • 129
  • 141