ios - 自定义多个标签或者按钮

  显示原文与译文双语对照的内容
0 0

在 objective-c 代码中自定义不同的标签和 UIButtons,但不知道如何将它们一起编辑,例如:


[[CincKmButton layer] setCornerRadius:10];
[CincKmButton setClipsToBounds:YES];
[[CincKmButton layer] setBorderColor:
 [[UIColor colorWithRed:0 green:0 blue:0 alpha:1] CGColor]];
[CincKmButton setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"pickerBackground.jpg"]]];
[[CincKmButton layer] setBorderWidth:2.75];

[[DeuKmButton layer] setCornerRadius:10];
[DeuKmButton setClipsToBounds:YES];
[[DeuKmButton layer] setBorderColor:
 [[UIColor colorWithRed:0 green:0 blue:0 alpha:1] CGColor]];
[DeuKmButton setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"pickerBackground.jpg"]]];
[[DeuKmButton layer] setBorderWidth:2.75];

[[HalfButton layer] setCornerRadius:10];
[HalfButton setClipsToBounds:YES];
[[HalfButton layer] setBorderColor:
 [[UIColor colorWithRed:0 green:0 blue:0 alpha:1] CGColor]];
[HalfButton setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"pickerBackground.jpg"]]];
[[HalfButton layer] setBorderWidth:2.75];

我如何在没有repating的情况下进行? 谢谢!

时间: 原作者:

0 0

最简单的方法可能是创建按钮的NSArray 和执行定制的方法。 它最终会看起来像


...
for (UIButton *button in @[CincKmButton,DeuKmButton,HalfButton]) {
 [self configureButton:button]
}
...

- (void) configureButton:(UIButton *)button {
 [button setClipsToBounds:YES];
 button.layer.cornerRadius = 10;
 button.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1].CGColor;
 button.layer.borderWidth = 2.75;
 [button setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"pickerBackground.jpg"]]];
}

0 0

创建 UIButton的子类并在该类中编写这些属性。

并使用创建的类而不是 UIButton

或者


[[UIButton appearance] setClipsToBounds:YES];
[[UIButton appearance].layer setCornerRadius:10.0];
//similarly apply all properties to appearance class, this will automatically get applied to all UIButtons 

原作者:
0 0

你可以制作一个方法并将该按钮发送给它。


-(void) assignButtonProperties :(UIButton *)sampleButton
{
 [[sampleButton layer] setCornerRadius:10];
 [sampleButton setClipsToBounds:YES];
 [[sampleButton layer] setBorderColor: [[UIColor colorWithRed:0 green:0 blue:0 alpha:1] CGColor]];
 [sampleButton setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"pickerBackground.jpg"]]];
 [[sampleButton layer] setBorderWidth:2.75];
}

例如:


[self assignButtonProperties : CincKmButton];
[self assignButtonProperties : DeuKmButton];

...