Question

J'ai déjà des charges cherchai et ne pouvait pas trouver une réponse.

J'ai un UILabel normal défini ainsi:

    UILabel *totalColors = [[[UILabel alloc] initWithFrame:CGRectMake(5, 7, 120, 69)] autorelease];
    totalColors.text = [NSString stringWithFormat:@"%d", total];
    totalColors.font = [UIFont fontWithName:@"Arial-BoldMT" size:60];
    totalColors.textColor = [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1.0];
    totalColors.backgroundColor = [UIColor clearColor];
    [self addSubview:totalColors];

Je voulais l'espacement horizontal entre les lettres, pour être plus serré, tandis que mantaining la taille des caractères.

Est-il possible de le faire? Il devrait être une chose assez simple à faire.

Vive les gars, André

Mise à jour:

Alors j'ai été obligé de le faire comme ceci:

- (void) drawRect:(CGRect)rect 
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSelectFont (context, "Arial-BoldMT", 60, kCGEncodingMacRoman);
    CGContextSetCharacterSpacing (context, -10);
    CGContextSetTextDrawingMode (context, kCGTextFill);

    CGContextSetRGBFillColor(context, 221/255.0, 221/255.0, 221/255.0, 221/255.0);
    CGAffineTransform xform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
    CGContextSetTextMatrix(context, xform);

    char* result = malloc(17);
    sprintf(result, "%d", totalNumber);

    CGContextShowTextAtPoint (context, 0, 54, result, strlen(result));
}

Mais je dois aligner ce à droite. Je pouvais le faire manuellement si je savais que la largeur du texte élaboré, mais il est la preuve presque impossible de trouver cela.

Je l'ai lu ATSU, mais je ne pouvais pas trouver des exemples.

Cette suce: /

Était-ce utile?

La solution 3

Je suis venu avec une solution pour l'espacement des lettres et l'alignement à droite.

Ici, il va:

    NSString *number = [NSString stringWithFormat:@"%d", total];

    int lastPos = 85;

    NSUInteger i;
    for (i = number.length; i > 0; i--)
    {
        NSRange range = {i-1,1};
        NSString *n = [number substringWithRange:range];

        UILabel *digit = [[[UILabel alloc] initWithFrame:CGRectMake(5, 10, 35, 50)] autorelease];
        digit.text = n;
        digit.font = [UIFont fontWithName:@"Arial-BoldMT" size:60];
        digit.textColor = [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1.0];
        digit.backgroundColor = [UIColor clearColor];
        [self addSubview:digit];

        CGSize textSize = [[digit text] sizeWithFont:[digit font]];
        CGFloat textWidth = textSize.width;

        CGRect rect = digit.frame;
        rect.origin.x = lastPos - textWidth;
        digit.frame = rect;

        lastPos = rect.origin.x + 10;
    }

L'espacement des lettres est le « 10 » sur la dernière ligne. L'alignement provient des lastPos.

Espérons que cela aide quelqu'un là-bas.

Autres conseils

A partir de iOS 6, vous pouvez utiliser NSAttributedString dans UILabel.

Dans la chaîne attribuée, vous pouvez utiliser l'attribut NSKernAttributeName pour définir interlettrage

NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString: @"Test test test test "];
[attrStr addAttribute:NSKernAttributeName value:@(4.0) range:NSMakeRange(0, attrStr.length)];

UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, 300, 100)];
label.attributedText = attrStr;

J'ai étendu UILabel changer l'espacement des caractères. Cela devrait fonctionner la boîte et la police tire, texte, couleur etc du UILabel lui-même (bon codage!).

Vous remarquerez peut-être que je dessine le texte deux fois, d'abord avec la couleur claire. Ce centre est automatique, le texte de l'étiquette. Bien que cela puisse être inefficace - n'est pas agréable d'être autocentré

Amusez-vous!

@interface RALabel : UILabel {
}
@end  

@implementation RALabel 

- (void) drawRect:(CGRect)rect 
{

    // Drawing code

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSelectFont (context, [self.font.fontName cStringUsingEncoding:NSASCIIStringEncoding], self.font.pointSize, kCGEncodingMacRoman);
    CGContextSetCharacterSpacing(context, 1);
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGAffineTransform myTextTransform = CGAffineTransformScale(CGAffineTransformIdentity, 1.f, -1.f );
    CGContextSetTextMatrix (context, myTextTransform);

    // draw 1 but invisbly to get the string length.
    CGPoint p =CGContextGetTextPosition(context);
    float centeredY = (self.font.pointSize + (self.frame.size.height- self.font.pointSize)/2)-2;
    CGContextShowTextAtPoint(context, 0, centeredY, [self.text cStringUsingEncoding:NSASCIIStringEncoding], [self.text length]);
    CGPoint v =CGContextGetTextPosition(context);

    // calculate width and draw second one.
    float width = v.x - p.x;
    float centeredX =(self.frame.size.width- width)/2;
    CGContextSetFillColorWithColor(context, [self.textColor CGColor]);
    CGContextShowTextAtPoint(context, centeredX, centeredY, [self.text cStringUsingEncoding:NSASCIIStringEncoding], [self.text length]);

}

Swift:

let myTitle = "my title"
let titleLabel = UILabel()
let attributes: NSDictionary = [
    NSFontAttributeName:UIFont(name: "HelveticaNeue-Light", size: 20),
    NSForegroundColorAttributeName:UIColor.whiteColor(),
    NSKernAttributeName:CGFloat(2.0)
]
let attributedTitle = NSAttributedString(string: myTitle, attributes: attributes as? [String : AnyObject])

titleLabel.attributedText = attributedTitle
titleLabel.sizeToFit()

Pas dans une version publique de l'iPhone OS. ;-) Si vous êtes un développeur iPhone actuel, vous pouvez avoir une idée de l'endroit où iPhone OS va en regardant à travers les « Nouveautés » Notes pour iPhone OS 3.2.

Mise à jour: iOS v3.2, qui a ajouté le support pour crénage, était encore sous NDA quand je posté. Pour une réponse mise à jour à jour, consultez Comment configurer Crénage iPhone UILabel .

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top