2013-07-30 69 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

相關問題