#import <UIKit/UIKit.h>
@interface BYTopLab : UILabel
@end
#import "BYTopLab.h"
@implementation BYTopLab
- (id)initWithFrame:(CGRect)frame {
return [super initWithFrame:frame];
}
-(void)drawTextInRect:(CGRect)requestedRect {
CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
[super drawTextInRect:actualRect];
}
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
textRect.origin.y = bounds.origin.y;
return textRect;
}
@end
调用:
/**正文*/
@property (nonatomic, strong)BYTopLab *textLab;
_textLab = [[BYTopLab alloc]initWithFrame:CGRectZero];
_textLab.numberOfLines = 0;
[self.contentView addSubview:_textLab];
[_textLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(ws.tagLab.mas_top).offset(-10);
make.top.equalTo(ws.readBtn.mas_bottom).offset(10);
make.left.width.equalTo(ws.titleLab);
}];
UILable的其他(与本文无关)
//设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏,解决UILabel文本竖直居中
NSDictionary *attrDict01 = @{NSBaselineOffsetAttributeName: @(25)};
_textLab.attributedText = [[NSAttributedString alloc] initWithString: _cellModel.digest attributes: attrDict01];
网友评论