2011-04-09 135 views
2

我有一个很大的问题,我有一个UILabel编程创建,然后通过接口生成器连接,在那里我已定位在我需要的位置,但我看到我设置它的文本它打印在中心在UILabelBox,我发现了很多问题,但我已经不知道我可以使用它,我发现这一点:UILabel TextAlignement垂直顶部

// 
// 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 

任何人都可以帮助我,我如何使用它吗?

回答

21

我知道这可能会迟到回答,但我没有这样说:
我做了一个类别的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 
+0

谢谢艾玛德,像一个魅力工作! – ToddH 2011-11-30 00:19:51

+1

只是为了防止任何人使用它并发现它不起作用:UILabel的文本必须在设置对齐之前进行设置,否则计算将在没有文本的情况下进行。只是说:-) – 2012-01-26 10:08:50

+0

谢谢,蒙克恩。这工作:P我认为这是足够的编码今晚! – OlivaresF 2012-04-22 04:13:43

6

可以按如下方式做到这一点.......

  1. 设置标签的numberOfLines财产0从IB

  2. 设置标签的lineBreakModeUILineBreakModeWordWrap(非常非常重要)

现在无论你在标签上设置什么,只需在其上附加几个@“\ n”..... ex.-

[yourTextLabel setText:@"myLabel\n\n\n\n\n"]; 
+0

这个问题几乎让我疯狂,直到找到你的解决方案!谢谢!无论如何,苹果应该实现一个功能,以对齐标签... – Tom 2013-03-07 00:10:12

+0

不错,最好的作弊... – 2013-10-17 20:43:45