Cocoa touch UILabel文本对齐垂直顶部
我有一个很大的问题,我用编程方式创建了一个Cocoa touch UILabel文本对齐垂直顶部,cocoa-touch,ios,uilabel,Cocoa Touch,Ios,Uilabel,我有一个很大的问题,我用编程方式创建了一个UILabel,然后通过Interface builder进行连接,我将其定位在我需要的位置,但我看到我在其中设置的文本打印在UILabelBox的中心,我发现了很多问题,但我不知道我可以使用它,我发现了以下几点: // // VerticallyAlignedLabel.h // #import <Foundation/Foundation.h> typedef enum VerticalAlignment { Vertica
UILabel
,然后通过Interface builder进行连接,我将其定位在我需要的位置,但我看到我在其中设置的文本打印在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
//
//垂直校准标签
//
#进口
typedef枚举垂直对齐{
垂直对齐顶部,
垂直排列中间,
垂直对齐底部,
}垂直排列;
@接口VerticallyAlignedLabel:UILabel{
@私人的
垂直对齐垂直对齐;
}
@属性(非原子,赋值)垂直对齐垂直对齐;
@结束
//
//VerticallyAlignedLabel.m
//
#导入“VerticallyAlignedLabel.h”
@实现VerticallyAlignedLabel
@合成垂直对齐=垂直对齐;
-(id)initWithFrame:(CGRect)帧{
if(self=[super initWithFrame:frame]){
自垂直对齐=垂直对齐中间;
}
回归自我;
}
-(void)设置垂直对齐:(垂直对齐)垂直对齐{
垂直对齐=垂直对齐;
[自我设置需要显示];
}
-(CGRect)textRectForBounds:(CGRect)界限限制为行数:(NSInteger)行数{
CGRect textRect=[super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
开关(自垂直对齐){
机箱垂直对齐顶部:
textRect.origin.y=bounds.origin.y;
打破
机箱垂直对齐底部:
textRect.origin.y=bounds.origin.y+bounds.size.height-textRect.size.height;
打破
案例垂直排列中间:
//失败。
违约:
textRect.origin.y=bounds.origin.y+(bounds.size.height-textRect.size.height)/2.0;
}
返回textRect;
}
-(void)drawTextInRect:(cRect)requestedRect{
CGRect ACTIVALRECT=[self-textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
[超级drawTextInRect:actualRect];
}
@结束
任何人都可以帮我如何使用它吗?你可以按如下方式操作
numberOfLines
属性0lineBreakMode
设置为uilinebreakmodewrap
(非常重要)我知道回答可能晚了,但我是这样做的:
我为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
//UILabel(VAlign).h
#进口
@接口UILabel(有效)
-(无效)设置垂直对齐顶部;
@结束
//UILabel(有效).m
#导入“UILabel(VAlign).h”
@实现UILabel(VAlign)
-(无效)设置垂直对齐顶部
{
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,
文本大小、高度);
[自设置帧:textRect];
[自我设置需要显示];
}
@结束
以防万一有人使用它并发现它不起作用:在设置对齐方式之前,必须先设置UILabel的文本,否则将不对任何文本进行计算。只是说:-)谢谢,芒肯。成功了:P我想今晚的编码已经足够了!这太完美了!很好的解决方案。你的解决方案对我也很有效。我想补充一点,我使用的类别越多,我就越喜欢它们!如果不调用一个新的方法,就不能对所有标签自动执行吗?这个问题几乎让我发疯,直到找到你的解决方案!谢谢无论如何,苹果应该实现一个功能来对齐标签。。。
// 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