0
我想實現三個視圖的自定義佈局。該視圖用於「酒吧」,「俱樂部」和「食物」類別。實施與故事板的自定義視圖佈局
這些類別中的每一個都會擁有自己的自定義圓視圖。內圓視圖將是具有文本標籤。
當前設置
目前三個視圖被添加到故事板,並給予相關的UIView
子類。子類然後處理使它們成爲圓圈並添加標籤。
- (id)init {
self = [super init];
if (self) {
[self setupView];
}
return self; }
-(void)setupView {
self.translatesAutoresizingMaskIntoConstraints = NO;
float newRadius = self.frame.size.width/2;
self.layer.cornerRadius = newRadius; // TODO/TEST - The UIImageView is set to 40x40 and the frame is yet to be created
self.layer.masksToBounds= YES;
self.layer.borderWidth = 5;
self.layer.borderColor = [UIColor colorWithRed:0.138 green:0.225 blue:1.000 alpha:1.000].CGColor;
self.backgroundColor = [self randomColor];
[self setupLabel];
}
-(void)setupLabel {
UILabel *label = [[UILabel alloc]init];
label.translatesAutoresizingMaskIntoConstraints = NO;
label.text = @"Test";
label.textColor = [UIColor whiteColor];
label.text = [label.text uppercaseString];
[self addSubview:label];
[self addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
}
然而,與上面的設置我無法想象我將如何實現與不同的標籤文本每圈有何看法?
什麼是處理這種自定義視圖和佈局的最佳方式。代碼中完全創建圓視圖並更改自定義init方法以傳遞標籤的NSString更好嗎?
這不工作我,這是正確的?當註銷標籤框架時,我得到0,0,並且在註銷框架文本時,我得到空值,但是我一直按照這個步驟進行。 – StuartM
self.text在哪裏設置? – StuartM
是的,self.text實際上並不需要,請更新'self.mylabel.text = value;' – StuartM