编辑 1
这个问题在这里得到了回答抱歉打扰了。
编辑 1
编辑 0
我在下面包含了更多代码以及结果图像,它显示了我得到的结果,这与我在没有子类化的情况下得到的结果相同,只有普通标签。在前 3 行中,标签似乎完全覆盖了按钮,而在第 4 行中没有标签,请注意按钮 #2 在第 4 行缩进。这是怎么回事。
此代码仅适用于四行中的第一行,但其他 3 行类似。
self.cardList = [NSMutableArray arrayWithCapacity:0];
self.yHonorsOrigin = 100;
self.xHonorsOrigin = 100;
self.xHonorsStep = 40.0;
self.xHonorsCurrent = self.xHonorsOrigin;
for( int x=0;x<[cards length]; x++ ){
[cards substringWithRange:NSMakeRange(x,1)];
UIButton *b= [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.xHonorsCurrent += self.xHonorsStep;
[b setTitle:[cards substringWithRange:NSMakeRange(x,1)] forState:UIControlStateNormal];
[b setTitle:@" " forState:UIControlStateDisabled];
[b setFrame:CGRectMake(self.xHonorsCurrent, self.yHonorsOrigin, 20, 20)];
[b setEnabled:YES];
[b setUserInteractionEnabled:YES];
[self.view addSubview:b];
[b addTarget:self action:@selector(spadeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
self.xHonorsCurrent = self.xHonorsOrigin + self.xHonorsStep/2;
for( int x=0;x<[cards length]-1; x++ ){
self.xHonorsCurrent += self.xHonorsStep;
UILabel *lab = [self valueForKey:@"theSuit" ];
lab.text = @"\u2660";
//lab.tintColor = [UIColor clearColor];
lab.center = CGPointMake(self.xHonorsCurrent, self.yHonorsOrigin);
[self.view addSubview:lab];
}
}
}
编辑 0
我曾尝试使用 对 UILabel 类进行子类化BDBoundedLabel
,但抛出了一个异常,即“子类标签类不符合 key 的键值编码theSuit
”。请帮忙。
BDBoundedLabel.m
#import "BDBoundedLabel.h"
@implementation BDBoundedLabel
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)drawTextInRect:(CGRect)rect{
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextStrokeRect(context, CGRectInset(self.bounds, 1, 1));
[super drawTextInRect:CGRectInset(rect, 5.0, 5.0)];
}
@end
视图控制器.h
#import <UIKit/UIKit.h>
#import "BDViewController.h"
#import "BDBoundedLabel.h"
@interface BDnameViewController : UIViewController{
IBOutlet BDBoundedLabel* theSuit;
}
视图控制器.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.cardList = [NSMutableArray arrayWithCapacity:0];
self.yHonorsOrigin = 100;
self.xHonorsOrigin = 100;
self.xHonorsStep = 40.0;
self.xHonorsCurrent = self.xHonorsOrigin;
// Do any additional setup after loading the view.
self.xHonorsCurrent = self.xHonorsOrigin + self.xHonorsStep/2;
for( int x=0;x<[cards length]-1; x++ ){
self.xHonorsCurrent += self.xHonorsStep;
UILabel *lab = [BDBoundedLabel valueForKey:@"theSuit" ];
lab.text = @"\u2660";
lab.center = CGPointMake(self.xHonorsCurrent, self.yHonorsOrigin);
[self.view addSubview:lab];
}
}