我有一個自定義視圖類,它繼承自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 。
有人知道我在做什麼錯嗎?
在此先感謝!