Pie*_*ero 2 cocoa-touch uilabel ios
我有一个很大的问题,我有一个以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
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我,我可以使用它吗?
Ema*_*mad 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
Run Code Online (Sandbox Code Playgroud)
你可以这样做.......
numberOfLines从IB 设置标签的属性0
将您的标签设置lineBreakMode为UILineBreakModeWordWrap(非常非常重要)
现在,无论你在标签上设置什么,只需在它上面添加几个@"\n"..... ex.-
[yourTextLabel setText:@"myLabel\n\n\n\n\n"];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9968 次 |
| 最近记录: |