UILabel垂直顶部对齐的解决

#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];


©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,694评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,323评论 25 708
  • 问答题 简单描述下web 服务器、PHP、数据库、浏览器是如何实现动态网站的 web 服务器:通过本机配置好的DN...
    嘿菠萝阅读 270评论 0 0
  • 音符跳跃的仙境 住着好客的猫头鹰 情绪开始流浪的光景 拖着长裙走进 睁大眼睛 让翠绿风景倒映 一汪碧泉 斑斓光影 ...
    Freesia3阅读 373评论 0 3
  • 原文链接:http://androidweekly.net/issues/issue-194 点击订阅邮箱第一时间...
    AndroidWeekly阅读 386评论 0 1

友情链接更多精彩内容