是否可以使用Cocoa Touch改变字体的字母间距/字距调整? [英] Is it possible to alter the letter-spacing/kerning of a font with Cocoa Touch?

查看:136
本文介绍了是否可以使用Cocoa Touch改变字体的字母间距/字距调整?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在互联网上搜索了一段时间,以获取有关如何改变UIKit中字体的字母间距/字距调整的信息。

I've been scouring the Internet for a while now for information on how one can alter the letter-spacing/kerning of a font within UIKit.

我的恐惧是的,就像使用自己的自定义字体一样,你根本就不能。这将是一个可怕的消息。

My fear is, that like using your own custom fonts, you simply can't. Which would be terrible news.

我知道Apple正在通过这些限制来保护我们免受糟糕的设计,但它们也阻止我们实现真正伟大的设计。

I know Apple is protecting us from bad design with these constraints, but they're also preventing us from implementing really great design too.

人们建议我做什么?


  1. 使用带有的字体标准字距调整,人们可能不会注意到差异。

  1. Use the font with the standard kerning, people probably wouldn't notice the difference anyway.

找到某人的黑客课程,以便在用他们辛苦赚来的现金分手后获得用户应得的外观。

Find someone's hacked class to get the look the user deserves after parting with their hard earned cash.

使用我忽略的方法,并用UIKit做到这一点,并向那个传授这隐藏知识的人致以永远的感激之情。

Use the method that I've somehow overlooked, and do it with UIKit, pledging my eternal gratitude to the person who imparts this hidden nugget of knowledge.


推荐答案

我从谷歌到这里试图用UITextField做同样的事情。我实现了下面对UILabel一般工作的东西。

I landed here from Google trying to do the same with UITextField. I implemented something that'll work generically for UILabel below.

@interface AdjustableUILable : UILabel {
    CGFloat characterSpacing;
}

@property CGFloat characterSpacing;

@end


@implementation AdjustableUILable

@synthesize characterSpacing;

- (void)drawTextInRect:(CGRect)rect
{    
    if (characterSpacing)
    {
        // Drawing code
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGFloat size = self.font.pointSize;

        CGContextSelectFont (context, [self.font.fontName UTF8String], size, kCGEncodingMacRoman);
        CGContextSetCharacterSpacing (context, characterSpacing);
        CGContextSetTextDrawingMode (context, kCGTextFill);

        // Rotate text to not be upside down
        CGAffineTransform xform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
        CGContextSetTextMatrix(context, xform);
        const char *cStr = [self.text UTF8String];
        CGContextShowTextAtPoint (context, rect.origin.x, rect.origin.y + size, cStr, strlen(cStr));
    }
    else
    {
        // no character spacing provided so do normal drawing
        [super drawTextInRect:rect];
    }
}

@end

然后使用它,只需在viewDidLoad中设置标签或其他东西

Then to use it, just set up the label in viewDidLoad or something

- (void)viewDidLoad
{
        myLabel.characterSpacing = 2; // point size
}

我尝试使用UITextField,但不幸的是它只添加了用户编辑字段后的字符间距。 -drawTextInRect仅在-textFieldShouldEndEditing委托方法之后调用。从其他论坛中有人建议这是因为UITextField需要知道字体的渲染才能显示光标。从来没有为这件作品找到解决方案...

I tried it with UITextField, but unfortunately it only adds the character spacing after the user has edited the field. -drawTextInRect is only called after the -textFieldShouldEndEditing delegate method. From other forums some suggested this was because UITextField needs to know the rendering of the font in order to display the cursor. Never found a solution for that piece...

这篇关于是否可以使用Cocoa Touch改变字体的字母间距/字距调整?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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