UILabel尺寸的自适应解决方法,如图所示,每张图片下方有描述图片的label,label的高度是自适应的,效果如图。
实现代码:
UILabel
*titleLabel = [[
UILabel
alloc
]
init
];
titleLabel. backgroundColor = [ UIColor colorWithRed : 1 green : 1 blue : 1 alpha : 0.5 ];
titleLabel. text = data. title ;
titleLabel. backgroundColor = [ UIColor colorWithRed : 1 green : 1 blue : 1 alpha : 0.5 ];
titleLabel. text = data. title ;
titleLabel.textAlignment = NSTextAlignmentCenter;
//自动换行
titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
//多行
titleLabel.
numberOfLines
=
0
;
titleLabel. font = [ UIFont systemFontOfSize : 10 ];
titleLabel. font = [ UIFont systemFontOfSize : 10 ];
//获得文字的高度
float
labelH = [titleLabel.
text
boundingRectWithSize
:
CGSizeMake
(viewW,
MAXFLOAT
)
options
:
NSStringDrawingUsesLineFragmentOrigin
attributes
:
@{
NSFontAttributeName
: [
UIFont
systemFontOfSize
:
10
]
}
context
:
nil
].
size
.
height
;
titleLabel.frame = CGRectMake(SPACE/2, viewH - labelH, viewW, labelH);