UILabel TextAlignement Вертикальный топ - PullRequest
2 голосов
/ 09 апреля 2011

У меня большая проблема, у меня UILabel, созданный программным способом, и затем соединяющийся через Интерфейсный конструктор, где я расположился там, где мне нужно, но я вижу, что текст, который я установил в нем, напечатан в центре UILabelBox, я нашел много вопросов, но я не знаю, могу ли я его использовать, я нашел это:

//
//  VerticallyAlignedLabel.h
//

#import <Foundation/Foundation.h>

typedef enum VerticalAlignment {
    VerticalAlignmentTop,
    VerticalAlignmentMiddle,
    VerticalAlignmentBottom,
} VerticalAlignment;

@interface VerticallyAlignedLabel : UILabel {
@private
    VerticalAlignment verticalAlignment_;
}

@property (nonatomic, assign) VerticalAlignment verticalAlignment;

@end

//
//  VerticallyAlignedLabel.m
//

#import "VerticallyAlignedLabel.h"


@implementation VerticallyAlignedLabel

@synthesize verticalAlignment = verticalAlignment_;

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.verticalAlignment = VerticalAlignmentMiddle;
    }
    return self;
}

- (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {
    verticalAlignment_ = verticalAlignment;
    [self setNeedsDisplay];
}

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
    CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
    switch (self.verticalAlignment) {
        case VerticalAlignmentTop:
            textRect.origin.y = bounds.origin.y;
            break;
        case VerticalAlignmentBottom:
            textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
            break;
        case VerticalAlignmentMiddle:
            // Fall through.
        default:
            textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;
    }
    return textRect;
}

-(void)drawTextInRect:(CGRect)requestedRect {
    CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
    [super drawTextInRect:actualRect];
}

@end

Кто-нибудь может мне помочь, как я могу его использовать, пожалуйста?

Ответы [ 2 ]

21 голосов
/ 24 июля 2011

Я знаю, что может быть поздно, чтобы ответить, но я сделал это так:
Я создал категорию для UILabel и позвонил -setVerticalAlignmentTop, когда это было необходимо.

// UILabel(VAlign).h
#import <Foundation/Foundation.h>
@interface UILabel (VAlign)
- (void) setVerticalAlignmentTop;
@end

// UILabel(VAlign).m
#import "UILabel(VAlign).h"
@implementation UILabel (VAlign)
- (void) setVerticalAlignmentTop
{
    CGSize textSize = [self.text sizeWithFont:self.font
                            constrainedToSize:self.frame.size
                                lineBreakMode:self.lineBreakMode];

    CGRect textRect = CGRectMake(self.frame.origin.x,
                                 self.frame.origin.y,
                                 self.frame.size.width,
                                 textSize.height);
    [self setFrame:textRect];
    [self setNeedsDisplay];
}

@end
6 голосов
/ 11 апреля 2011

Вы можете сделать это следующим образом .......

  1. Установите свойство numberOfLines вашего ярлыка 0 из IB

  2. Установите lineBreakMode вашего ярлыка как UILineBreakModeWordWrap (очень и очень важно)

Теперь, что бы вы ни указали на ярлыке, просто добавьте к нему несколько @ "\ n" ..... бывший .-

[yourTextLabel setText:@"myLabel\n\n\n\n\n"];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...