我有一个自定义视图类,它继承自UIView
。这个班级有一个UILabel
作为子视图。在init
-function这个自定义视图类的我设置了需要这样的东西:更新未知大小的多行UILabel(自动布局)的preferredMaxLayoutWidth
//h-file
#import <UIKit/UIKit.h>
@interface MyCustomView : UIView
@property (strong, nonatomic) UILabel *myLabel;
@end
//m-file
@implementation MyCustomView
@synthesize myLabel = _myLabel;
- (id)init
{
self = [super init];
if (self) {
_myLabel = [UILabel new];
if(_textView){
_myLabel.highlightedTextColor = [UIColor whiteColor];
_myLabel.translatesAutoresizingMaskIntoConstraints = NO;
_myLabel.lineBreakMode = NSLineBreakByWordWrapping;
_myLabel.numberOfLines = 0;
_myLabel.backgroundColor = [UIColor clearColor];
[self addSubview:_myLabel];
}
}
return self;
}
@end
我还设置了一堆限制的管理填充我的自定义视图中 - 而且有约束布局多012- 垂直轴和水平轴的实例也是如此。
要获得multilined标签输出我必须设置的UILabel
myLabel
的preferredMaxLayoutWidth
- 属性。宽度取决于可用空间。在http://www.objc.io/issue-3/advanced-auto-layout-toolbox.html我读到了,我可以让自动布局先计算出宽度,并在MyCustomView
-instance(此时单个内部的标签)已被设置之后将其设置为preferredMaxLayoutWidth
。
如果我把下面的函数到MyCustomView
,标签仍然有文字的一行:
- (void)layoutSubviews
{
[super layoutSubviews];
float width = _myLabel.frame.size.width;
_myLabel.preferredMaxLayoutWidth = width;
[super layoutSubviews];
}
如果我设置了preferredMaxLayoutWidth
到init
-function内显式值,标签multilined 。
有人知道我在做什么错吗?
在此先感谢!