UITextFieldで同じことをしようとして、Googleからここに着陸しました。以下のUILabelで一般的に機能するものを実装しました。
@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などでラベルを設定するだけです
- (void)viewDidLoad
{
myLabel.characterSpacing = 2; // point size
}
UITextFieldで試してみましたが、残念ながら、ユーザーがフィールドを編集した後に文字間隔を追加するだけです。-drawTextInRect は、-textFieldShouldEndEditing デリゲート メソッドの後にのみ呼び出されます。他のフォーラムから、これは UITextField がカーソルを表示するためにフォントのレンダリングを知る必要があるためであると示唆されました。その部分の解決策を見つけたことはありません...