美文网首页
UILabel垂直顶部对齐的解决

UILabel垂直顶部对齐的解决

作者: biyu6 | 来源:发表于2018-03-21 18:31 被阅读0次
#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];


相关文章

网友评论

      本文标题:UILabel垂直顶部对齐的解决

      本文链接:https://www.haomeiwen.com/subject/beqnqftx.html