【发布时间】:2014-05-07 14:31:39
【问题描述】:
我正在尝试为三个视图实现自定义布局。这些视图用于“酒吧”、“俱乐部”和“食物”类别。
每个类别都有自己的自定义圆形视图。在圆形视图中将是一个包含文本的标签。
当前设置
目前这三个视图被添加到 Storyboard 中,并被赋予了相关的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 会更好吗?
【问题讨论】:
标签: ios objective-c uiview uilabel