自定义字体.保持字体宽度相同 [英] Custom Font. Keeping the font width same

查看:42
本文介绍了自定义字体.保持字体宽度相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用石英2d绘制线.

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 there any way to make the space uniform!!!

推荐答案

通常每个字符都有不同的进阶,因此在绘制文本时应考虑到这一点.代替使用 j ,执行以下操作:

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

extraSpacing 设置为零,以获取字体的默认间距.

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

此外,我不确定您使用的是什么属性,但是为什么不只使用 NSAttributedString (或 CF * )呢?

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

这篇关于自定义字体.保持字体宽度相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆