6

ユーザーが のサイズと向きを変更できるアプリの作成を検討していますUITextField。Aviary アプリを調べていたところ、ユーザーはテキストのサイズを増減できるだけでなく、向きも変更できることがわかりました。

ここに画像の説明を入力

だから私が聞きたい質問は

1)彼らCoreTextはこれを行うために使用していますUILabelUIText

2) 動的にサイズを変更する方法がわかりません。彼らは使用していUIViewますか?

3) CoreText のチュートリアル、例をグーグルで検索し、アップルのドキュメントとサンプル プロジェクトを確認し、ココア コントロールと github を調べましたが、これがどのように行われるかを示すサンプル コードやチュートリアルはまだ見当たりません。

誰かが私に何らかの方向性やチュートリアルを教えてくれるほど親切でしょうか?

4

1 に答える 1

7

フォントの外観に関しては、いくつかのオプションがあります。

  1. 標準UILabelを使用して、ここに表示されているほとんどの効果 (フォント、サイズ、色、回転) を実現できます。標準を超えている 1 つの機能はUILabel、フォントの周りの白いストロークです。しかし、有効な iOS 6 では、UILabelそのattributedTextプロパティ ( NSAttributedString.

  2. 6.0 より前の iOS バージョンでは、テキストの周りに白い線の色を実現するには、CoreGraphicsまたはCoreTextを使用する必要がありました。

テキストの動的なサイズ変更、回転、および移動に関してはtransform、ビューのプロパティを調整するジェスチャ認識機能を使用していることは間違いありません (より正確には、おそらくtransform回転の調整、centerまたはframeのドラッグの調整、および と の両方のframeフォント サイズの調整)。サイズ変更用 (scale変換のみを使用すると、望ましくないピクセレーションが発生する可能性があります)。

スタック オーバーフローでジェスチャ レコグナイザーや、ビューのドラッグ、サイズ変更、回転に関する質問を検索すると、かなりのヒットが得られます。


iOS 6 では、赤いテキストにUILabel.

// create attributes dictionary

NSDictionary *attributes = @{
                             NSFontAttributeName            : [UIFont systemFontOfSize:48.0],
                             NSForegroundColorAttributeName : [UIColor redColor],
                             NSStrokeColorAttributeName     : [UIColor whiteColor],
                             NSStrokeWidthAttributeName     : @(-3)
                             };

// make the attributed string

NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:@"bigcat"
                                                                   attributes:attributes];

self.label.attributedText = stringToDraw;

iOS の属性付き文字列については、次を参照してください。


iOS 6 より前のバージョンの iOS をサポートする必要がある場合は、CoreText または CoreGraphics を使用する必要があります。CoreText レンディションは次のようになります。

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];

    if (!self.text)
        return;

    // create a font

    CTFontRef sysUIFont = CTFontCreateUIFontForLanguage(kCTFontSystemFontType, self.fontSize, NULL);

    // create attributes dictionary

    NSDictionary *attributes = @{
                                 (__bridge id)kCTFontAttributeName            : (__bridge id)sysUIFont,
                                 (__bridge id)kCTForegroundColorAttributeName : (__bridge id)[self.fillColor CGColor],
                                 (__bridge id)kCTStrokeColorAttributeName     : (__bridge id)[self.borderColor CGColor],
                                 (__bridge id)kCTStrokeWidthAttributeName     : @(-3)
                                 };

    // make the attributed string

    NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:self.text
                                                                       attributes:attributes];

    // begin drawing

    CGContextRef context = UIGraphicsGetCurrentContext();

    // flip the coordinate system

    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // create CTLineRef

    CTLineRef line = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)stringToDraw);

    // figure out the size (which we'll use to center it)

    CGFloat ascent;
    CGFloat descent;
    CGFloat width = CTLineGetTypographicBounds(line, &ascent, &descent, NULL);
    CGFloat height = ascent + descent;
    CGSize stringSize = CGSizeMake(width, height);

    // draw it

    CGContextSetTextPosition(context,
                             (self.bounds.size.width  - stringSize.width)  / 2.0,
                             (self.bounds.size.height - stringSize.height + descent) / 2.0);
    CTLineDraw(line, context);

    // clean up

    CFRelease(line);
    CFRelease(sysUIFont);
}

上記のより完全な実装は、私のGitHub CoreText Demonstrationにあります。


drawRectこれは、テキストの周りにアウトラインを付けてテキストを書き込む、非常に単純な Core Graphics の実装です。

@implementation CustomView

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];

    if (!self.text)
        return;

    // begin drawing

    CGContextRef context = UIGraphicsGetCurrentContext();

    // flip the coordinate system

    CGContextSetTextMatrix(context, CGAffineTransformMakeRotation(M_PI_4 / 2.0));
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // write the text

    CGContextSelectFont (context, "Helvetica", self.fontSize, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode (context, kCGTextFillStroke);
    CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]);
    CGContextSetLineWidth(context, 1.5);
    CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
    CGContextShowTextAtPoint (context, 40, 100, [self.text UTF8String], [self.text length]);
}

@end
于 2013-04-16T14:21:02.127 に答える