iOS开发UILabel顶端左对齐
2018-09-26 本文已影响18人
lczalh
这样写文字会显示在中间
UILabel *content = [[UILabel alloc]init];
[self.contentImageView addSubview:content];
content.numberOfLines = 0;
content.text = @"测试测试测试测试测试";
[content mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentImageView).offset(28);
make.top.equalTo(self.contentImageView).offset(20);
make.bottom.equalTo(self.contentImageView).offset(-20);
make.right.equalTo(self.contentImageView).offset(-28);
}];
顶端左对齐
TopLeftLabel *content = [[TopLeftLabel alloc]init];
[self.contentImageView addSubview:content];
content.numberOfLines = 0;
content.text = @"测试测试测试测试测试";
[content mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentImageView).offset(28);
make.top.equalTo(self.contentImageView).offset(20);
make.bottom.equalTo(self.contentImageView).offset(-20);
make.right.equalTo(self.contentImageView).offset(-28);
}];
#import "TopLeftLabel.h"
@implementation TopLeftLabel
- (id)initWithFrame:(CGRect)frame {
return [super initWithFrame:frame];
}
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
textRect.origin.y = bounds.origin.y;
return textRect;
}
-(void)drawTextInRect:(CGRect)requestedRect {
CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
[super drawTextInRect:actualRect];
}
@end