2011-04-09 51 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