自定义多个标签或按钮

时间:2015-02-13 14:19:11

标签: ios objective-c button label

在我的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];

我怎么能在没有每次重复的情况下做到这一点?谢谢!

4 个答案:

答案 0 :(得分:1)

最简单的方法可能是创建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"]]];
}

答案 1 :(得分:0)

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

使用Created Class代替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 

答案 2 :(得分:0)

你可以这样做:

CincKmButton.layer.cornerRadius = DeuKmButton.layer.cornerRadius  = 10;
CincKmButton.backgroundColor = DeuKmButton.backgroundColor  = [UIColor colorWithPatternImage:[UIImage imageNamed:@"pickerBackground.jpg"]];

或者创建一个UIButton的NSMutableArray并使用"同时编辑所有"

或者你可以创建一个UIButton子类来用方法设置所有这些属性。

答案 3 :(得分: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];