2013-07-30 68 views
0

我一直在关注CS193P Stanford iOS开发讲座,在第二个作业“Set”中,我无法弄清楚我的程序在按钮上显示一个属性字符串时出了什么问题。出于某种原因,当我的字符串尝试显示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 
+0

我们可以在这里看到一些代码,也许有一些截图,所以我们不必编译和筛选代码? – Undo

+0

@MatthewZhu检查我的答案NSAttributeString与另一编码..尝试的代码,让我从这个链接http://stackoverflow.com/questions/13579209/two-colors-for-uilabel-text/13579318#13579318 –

回答

1

有两个不同的问题,据我可以看到:

  • 按钮标签截断,所以你看到的“3个随机其他字符”是省略号(...)。您必须放大按钮或使用较小的字体大小。也许还可以将按钮的“换行符”模式设置为“剪辑”而不是“截断...”。
  • 在iOS 6上,某些字符(如“◼”符号)显示为“Apple Color Emoji”。 这是一个(有争议的讨论)“功能”,见例如。 https://devforums.apple.com/message/487463#487463(需要Apple Developer登录)。因此,颜色等属性被忽略。

    通过表情符号字符替换可以(从UILabel, UIFont and UTF-8 Triangle)通过附加 Unicode的“变异选择” U + FE0E被抑制:

    + (NSArray *)validSymbols { 
        return @[@"◼\uFE0E",@"●",@"▲"]; 
    } 
    

    与该改变,如预期所有的符号被正确显示。

+0

知道哇完美的答案。正是我在找什么。非常丰富。为什么在此之前,当按钮是空白,我被允许编辑属性编辑器的字体大小,但现在我不能?谢谢! – SwiftMatt

相关问题