我有一个很大的问题,我有一个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
任何人都可以帮助我,我如何使用它吗?
谢谢艾玛德,像一个魅力工作! – ToddH 2011-11-30 00:19:51
只是为了防止任何人使用它并发现它不起作用:UILabel的文本必须在设置对齐之前进行设置,否则计算将在没有文本的情况下进行。只是说:-) – 2012-01-26 10:08:50
谢谢,蒙克恩。这工作:P我认为这是足够的编码今晚! – OlivaresF 2012-04-22 04:13:43