我一直在关注 CS193P 斯坦福 iOS 开发讲座,在第二个作业“设置”中,我无法弄清楚我的程序在按钮上显示属性字符串有什么问题。出于某种原因,当我的字符串尝试显示 3 个字符时,它最终显示为 1 个大字符和 3 个随机其他字符。当我只打算有红色、绿色和蓝色字符时,许多字符也显示为黑色。如果有人可以构建这个项目并帮助我弄清楚发生了什么,我将不胜感激。我花了大约 4 个小时试图调试它。来源在 https://github.com/zhumatthew/MatchSet
问题出在标签栏中第二个标签的设置视图控制器下。
谢谢。
http://postimg.org/image/gs7qley3r/
#import "SetViewController.h"
#import "SetCardDeck.h"
#import "SetCard.h"
@interface SetViewController ()
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
@end
@implementation SetViewController
@synthesize deck = _deck;
- (Deck *)deck {
if (!_deck) _deck = [[SetCardDeck alloc] init];
return _deck;
}
- (void)updateUI {
[super updateUI];
for (UIButton *cardButton in self.cardButtons) {
UIColor *foregroundColor;
UIColor *strokeColor;
SetCard *card = (SetCard *)[self.game cardAtIndex:[self.cardButtons indexOfObject:cardButton]];
NSString *string = [[NSString alloc] init];
for (int i = 0; i < card.number; i++) {
string = [NSString stringWithFormat:@"%@%@", string, card.symbol];
}
if ([card.color isEqualToString:@"R"]) {
foregroundColor = [UIColor redColor];
} else if ([card.color isEqualToString:@"G"]) {
foregroundColor = [UIColor greenColor];
} else if ([card.color isEqualToString:@"B"]) {
foregroundColor = [UIColor blueColor];
}
strokeColor = [foregroundColor copy];
if ([card.shading isEqualToString:@"S"]) {
foregroundColor = [foregroundColor colorWithAlphaComponent:1];
} else if ([card.shading isEqualToString:@"H"]) {
foregroundColor = [foregroundColor colorWithAlphaComponent:0.3];
} else if ([card.shading isEqualToString:@"O"]) {
foregroundColor = [foregroundColor colorWithAlphaComponent:0];
}
NSAttributedString *mat = [[NSAttributedString alloc] initWithString:string attributes:@{NSForegroundColorAttributeName:foregroundColor,NSStrokeWidthAttributeName:@-5,NSStrokeColorAttributeName:strokeColor}];
[cardButton setAttributedTitle:mat forState:UIControlStateNormal];
CGFloat red;
CGFloat green;
CGFloat blue;
CGFloat alpha;
[foregroundColor getRed:&red green:&green blue:&blue alpha:&alpha];
NSLog(@"red:%f green:%f blue:%f alpha:%f index:%d",red,green,blue,alpha,[self.cardButtons indexOfObject:cardButton]);
[strokeColor getRed:&red green:&green blue:&blue alpha:&alpha];
NSLog(@"red:%f green:%f blue:%f alpha:%f ",red,green,blue,alpha);
NSLog(@"%@",[mat string]);
cardButton.selected = card.isFaceUp;
cardButton.enabled = !card.isUnplayable;
if (card.isFaceUp) {
[cardButton setBackgroundColor:[UIColor lightGrayColor]];
} else {
[cardButton setBackgroundColor:[UIColor whiteColor]];
}
}
}
@end