iOS自定义UILabel可以使文字居上居中居下

更新时间:2023-05-22 01:47:13 阅读: 评论:0

豫剧花木兰从军
iOS⾃定义UILabel可以使⽂字居上居中居下
前⾔:做过iOS 的都知道,UILabel有让⽂字居左,居右,居中的属性,可是有时候我们在做项⽬的时候,可能会需要将label上的⽂字居上, 居中或者 居下这样的操作。那么今天我们就来说⼀说这种效果的实现⽅式。
#import <UIKit/UIKit.h>
typedefenum
{
谢谢老板表情包VerticalAlignmentTop =0,// default
VerticalAlignmentMiddle,
宁波小吃VerticalAlignmentBottom,
} VerticalAlignment;
@interfaceBaUILabel :UILabel
{
@private
VerticalAlignment_verticalAlignment;
}
@property(nonatomic)VerticalAlignmentverticalAlignment;
@end
达克宁多少钱#import"BaUILabel.h"
@implementationBaUILabel
@synthesizeverticalAlignment =verticalAlignment_;
- (id)initWithFrame:(CGRect)frame {
if(lf= [superinitWithFrame:frame]) {
lf.verticalAlignment=VerticalAlignmentMiddle;戏答
}
returnlf;
}
- (void)tVerticalAlignment:(VerticalAlignment)verticalAlignment {
verticalAlignment_= verticalAlignment;
[lftNeedsDisplay];
}
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines { CGRecttextRect = [supertextRectForBounds:boundslimitedToNumberOfLines:numberOfLines];
switch(lf.verticalAlignment) {
caVerticalAlignmentTop:
break;
caVerticalAlignmentBottom:
break;
caVerticalAlignmentMiddle:
小熊猫车// Fall through.
default:
骑行新藏线}
returntextRect;
}
桃树嫁接时间和方法-(void)drawTextInRect:(CGRect)requestedRect {
CGRectactualRect = [lftextRectForBounds:requestedRectlimitedToNumberOfLines:lf.numberOfLines]; [superdrawTextInRect:actualRect];
}
@end
这样我们在使⽤的时候,只要继承这个label类,然后选择想要实现效果的属性即可

本文发布于:2023-05-22 01:47:13,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/726398.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:时候   居中   居下   效果
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图