iphone - 为什么我的属性字符串显示得很奇怪?

标签 iphone ios objective-c uibutton nsattributedstring

我一直在关注 CS193P 斯坦福 iOS 开发讲座,在第二个作业“Set”中,我无法弄清楚我的程序在按钮上显示属性字符串有什么问题。由于某种原因,当我的字符串尝试显示 3 个字符时,它最终显示为 1 个大字符和 3 个随机其他字符。当我只想有红色、绿色和蓝色字符时,许多字符显示为黑色。如果有人可以构建这个项目并帮助我弄清楚发生了什么,我将不胜感激。我花了大约 4 个小时尝试调试这个。来源位于 https://github.com/zhumatthew/MatchSet

问题出在选项卡栏中第二个选项卡的设置 View Controller 下。

谢谢。

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

最佳答案

据我所知,有两个不同的问题:

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

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

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

    完成此更改后,所有符号都会按预期正确显示。

关于iphone - 为什么我的属性字符串显示得很奇怪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17938398/

相关文章:

javascript - 为什么 Javascript 在 iPhone 上运行这么慢?

ios - 在整个应用程序中隐藏导航栏

iphone - 如何创建一个完整的 NSDate 对象来表示下一个指定的日期时间?

ios - 是否可以将手机麦克风的输入转换到接收器?

ios - 如何测试 iOS 设备网络是使用 IPv6 还是 IPv4?

ios - 如何处理 UICollectionView 中的后台点击

ios - iOS 11 中的 NSFoundationVersionNumber - 如何以编程方式检测 iOS 版本

objective-c - 覆盖 isEqual : and hash 的最佳实践

ios - 如何使用EventKit处理重复事件

ios - Xamarin:无法在 iOS 9.2.1 设备上运行应用程序