title: iOS: Draw a text at a point in a rectangle
body: <p>I'm using core text API to draw text on UI. (I don't know other methods, because most Google search results lead me to core text api).</p>
  
  <p>I have read some tutorials online about using core text API. In those tutorial, I see step by step to config from matrix, attribute string to framesetter ... but none of them explain carefully meaning of each step, so I cannot modify by myself.</p>
  
  <p>Below code is the function draw text on screen with <code>(x,y)</code> is the location which I want to draw. This piece of code describe clearly step by step do draw on screen. Nevertheless I don't know where to put x and y parameters, so text will start to draw at this point in rectangle.</p>
  
  <pre><code>// draw text on screen.
  + (void)drawText:(CGContextRef)context bound:(CGRect)rect text:(NSString *)text x:(float)x y:(float) y color:(UIColor *)color size:(float)textSize{
      CGContextSaveGState(context);
      // always remember to reset the text matrix before drawing.
      // otherwise the result will be unpredictable like using uninitialize memory
      CGContextSetTextMatrix(context, CGAffineTransformIdentity);
      CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0f, -1.0f));
      // Flip the coordinate system. because core Text uses different coordinate system with UI Kit
      CGContextTranslateCTM(context, 0, rect.size.height);
      CGContextScaleCTM(context, 1.0, -1.0);
  
      // step 1: prepare attribute string
      NSDictionary *attributes;
      attributes = @{
              (NSString *) kCTFontAttributeName            : [UIFont fontWithName:@"Helvetica" size:textSize],
              (NSString *) kCTForegroundColorAttributeName : color
      };
  
      NSAttributedString *str = [[NSAttributedString alloc]
                                 initWithString:text attributes:attributes];
  
      CFAttributedStringRef attrString = (__bridge CFAttributedStringRef)str;
  
      // step 2: create CTFFrameSetter
      CTFramesetterRef framesetter =
      CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrString);
  
      // step 3. create CGPath
      CGMutablePathRef path = CGPathCreateMutable();
      CGPathAddRect(path, NULL, rect);
  
      // step 4. get the frame
      // use the CGPath and CTFrameSetter to create CTFrame. then drawn it in currently context
      CTFrameRef frame = CTFramesetterCreateFrame
              (framesetter, CFRangeMake(0, 0), path, NULL);
  
      CTFrameDraw(frame, context);
  
      // step 5. release resource
      //CFRelease(attrString);
      CFRelease(framesetter);
      CFRelease(frame);
      CFRelease(path);
  
      CGContextRestoreGState(context);
  }
  </code></pre>
  
  <p>Moreover, I see there are function: <code>CGContextSetTextPosition</code> This seem what I need, but where should I put in above code. I have tried some, but no succeed. Please tell me how to fix this. </p>
  
  <p>Thanks :)</p>