使UILabel垂直考上对齐

来源:互联网 发布:c语言必背基础程序 编辑:程序博客网 时间:2024/05/01 11:47
调用sizeToFit方法后,宽度不会变,高度会随内容而改变。

可用lblPrompt.numberOfLines 来限制高度。

在iOS8、9上测试,如果设置numberOfLines为非0值,则行数不会超过numberOfLines指定的行数。

如果numberOfLines设为0,则高度会超过frame的设置。

一个问题是,此时的.lineBreakMode = NSLineBreakByTruncatingHead
表现的不对,会在最后一行的头部显示省略号,而不是第一行。


注意,要在每次给.text设值后都要调用sizeToFit方法。


1) Set the new text:


myLabel.text = @"Some Text"


2) Set the maximum number of lines to 0 (automatic):


myLabel.numberOfLines = 0


3) Set the frame of the label to the maximum size:


myLabel.frame = CGRectMake(20,20,200,800)


4) Call sizeToFit to reduce the frame size so the contents just fit:


[myLabel sizeToFit]
0 0
原创粉丝点击