美文网首页iOS Dev.iOS 开发 iOS Developer
可以实现文字排版四角对齐的Label

可以实现文字排版四角对齐的Label

作者: for_in | 来源:发表于2016-05-23 00:46 被阅读404次

奇葩的需求

在某技术群里面,某个童鞋遇到一需求,要求Label里面的文字实现左上角对齐,但UILabel只有左右中间三种,好奇葩的需求,自己找了点资料研究了一下,写了个继承UILabel的控件,源码在这里:https://github.com/HuangJiayong/HJYLabel

原理

点进UILabel的头文件可以看到这两个方法:

// override points. can adjust rect before calling super.
// label has default content mode of UIViewContentModeRedraw

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;
- (void)drawTextInRect:(CGRect)rect;

调用以下方法可以获得绘画文字的rect

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;

然后修改这个rect的位置
最后调用

- (void)drawTextInRect:(CGRect)rect;

重新绘制文字位置

相关文章

网友评论

本文标题:可以实现文字排版四角对齐的Label

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