开发者

Custom Font. Keeping the font width same

I am trying to draw a string using quartz 2d.

What i am doing is, i am drawing each letter of the string individually, because each letter has special attributes associated with it, by taking each letter into a new string.

The string gets printed, but the space between the letters is not uniform. It looks very ugly to read . I read someting about using custom fonts. But i have no Idea, if I can do it!! my code is here.

- (void) drawRect : (CGRect)rect{
  NSString *string=@"My Name Is Adam";
  float j=0;
  const char *charStr=[string cStringUsingEncoding: NSASCIIStringEncoding]; 

  for(int i=0;i<strlen(charStr);i++)


   {
      NSString *str=[NSString stringWithFormat:@"%c",charStr[i]];
      const char *s=[str cStringUsingEncoding:NSASCIIStringEncoding];
      NSLog(@"%s",s);   

      CGContextRef context=[self getMeContextRef];
      CGContextSetTextMatrix (context,CGAffineTransformMakeScale(1.0, -1.0)) ;
      CGContextSelectFont(context, "Arial", 24, kCGEncodingMacRoman);
     //CGContextSetCharacterSpacing (context, 10);



       CGContextSetRGBFillColor (context, 0,0,200, 1); 
       CGContextSetTextDrawingMode(context,kCGTextFill);
       CGContextShowTextAtPoint(context, 80+j,80,s,1);
       j=j+15;

}

}

In the output 'My Name is Adam' gets printed but the space between the letters is not uniform.!! is th开发者_开发问答ere any way to make the space uniform!!!


in general each character has a different advance, so you should account for it when drawing your text. Instead of using j, do the following:

// ...
CGContextSetTextPosition(initX,initY);
for(...){
    // ...
    CGPoint pos = CGContextGetTextPosition(context);
    CGContextSetTextPosition(pos.x+extraSpace,pos.y);
    CGContextShowText(context,s,1);
    // ...
}

set extraSpacing to zero to get the font's default spacing.

Also, I'm not sure what attributes you're using, but why don't you just use NSAttributedString (or CF*)?


Use a non-proportional/fixed width font such as Monaco or Courier. Let me know the results.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜