iPhone центр текста ShowTextAtPoint - PullRequest
4 голосов
/ 28 января 2010

Я ищу способ центрировать текст на iPhone с помощью метода context.ShowTextAtPoint ().

Ответы [ 2 ]

8 голосов
/ 28 декабря 2010

Лучше поздно, чем никогда:

Сначала поместите текст в представление (в невидимом режиме):

CGTextDrawingMode mode = CGContextGetTextDrawingMode(ctx);
CGContextSetTextDrawingMode(ctx, kCGTextInvisible);
CGContextShowTextAtPoint(ctx, 0, 0, @"test", strlen("test"));

Затем получите положение текста и установите режим обратно на видимый:

CGPoint pt = CGContextGetTextPosition(ctx);
CGContextSetTextDrawingMode(ctx, mode);

Теперь у вас есть позиция невидимого текста. Затем используйте центр экрана (160) и поместите на него новый текст.

CGContextShowTextAtPoint(ctx, 160 - pt.x / 2, 200, @"test", strlen("test"));
2 голосов
/ 17 ноября 2011

вот код для выравнивания влево и вправо. из него можно выяснить центр

#import <UIKit/UIKit.h>


@interface AdustableUILabel : UILabel{
    CGFloat characterSpacing;

}

@property CGFloat characterSpacing;


@end

#import "AdustableUILabel.h"

@implementation AdustableUILabel

@synthesize characterSpacing;

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

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

        CGContextSetRGBFillColor(context, 255/255.0, 255/255.0, 255/255.0, 255/255.0);


        // 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];
        //get the UILabel alignment
        if(self.textAlignment == UITextAlignmentLeft){
            // Drawing code
            //just draw it in the current Rect offset from LHS            

            CGContextShowTextAtPoint (context, rect.origin.x, rect.origin.y + size, cStr, strlen(cStr));
        }else{
            //RHS
            //draw in invisible mode, get the size then subtract from width of rect to get left hand x of the text
            CGContextSetTextDrawingMode(context, kCGTextInvisible);
            CGContextShowTextAtPoint (context, rect.origin.x, rect.origin.y + size, cStr, strlen(cStr));

            //Then get the position of the text and set the mode back to visible:            
            CGPoint pt = CGContextGetTextPosition(context);            
            //Draw at new position
            CGContextSetTextDrawingMode(context, kCGTextFill);
            CGContextShowTextAtPoint(context, rect.size.width - pt.x, rect.origin.y + size, cStr, strlen(cStr));
            //===============
        }        
    }
    else
    {
        // no character spacing provided so do normal drawing
        [super drawTextInRect:rect];
    }
}

@end
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...