ios - objective-c - 如何在数组中显示图像?

标签 ios objective-c arrays image

我试图在我的应用程序中显示图像而不是文本,因此我尝试将 UIImages 集成到我的文本数组中。糟糕的是,我每次尝试实现时都会出错,所以我决定询问 Objective-C 专业人士。需要并欢迎每一个帮助!

这是我的 .h 数组代码:

// array
@property (nonatomic, strong) NSArray *aFacts;

// pich a random fact
@property (NS_NONATOMIC_IOSONLY, readonly, copy) NSString *randomAFact;

和我的 .m 数组代码

-(instancetype) init {
    self = [super init];
    if (self) {
        _aFacts = @[@"Humans are the only primates that don’t have pigment in the palms of their hands.",
                         [UIImage imageNamed:@"refresh"],
];

    }
    return self;
    }

// arcm4
-(NSString *)randomAFact {
    int random = arc4random_uniform((int)self.aFacts.count);
    return (self.aFacts)[random];
}

@end

此代码显示在标签中。您认为标签会在阵列中显示成功的集成图像吗?

最佳答案

序言

要将文本添加到标签,您可以这样写(就像您现在所做的那样)。

self.label.text = @"This is a test";

要添加图像,请编写此代码。

NSTextAttachment * attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"refresh"];
self.label.attributedText = [NSAttributedString attributedStringWithAttachment:attachment];

因此我们应该对您的代码进行以下更改。

1。填充数组。

您不应将图像直接添加到数组中。添加一个 NSAttributedString 代替。所以你的初始化变成:

- (instancetype) init {
    self = [super init];
    if (self) {
        NSTextAttachment * attachment = [[NSTextAttachment alloc] init];
        attachment.image = [UIImage imageNamed:@"refresh"];
        NSAttributedString * attributedString = [NSAttributedString attributedStringWithAttachment:attachment];
        _aFacts = @[@"Humans are the only primates that don’t have pigment in the palms of their hands.", attributedString];
    }
    return self;
}

2。更新 randomAFact

删除您的 randomFact 方法并添加这个新版本。

- (void)populateLabel:(UILabel*)label {
    int random = arc4random_uniform(@(_aFacts.count).intValue);
    NSObject * element = _aFacts[random];
    if ([element isKindOfClass:NSString.class]) {
        NSLog(@"Will add string")          
        label.attributedText = nil;         
        label.text = (NSString*) element;
    } else if ([element isKindOfClass:NSAttributedString.class]) {
        NSLog(@"Will add image")
        label.text = nil;           
        label.attributedText = (NSAttributedString*)element;
    } else {
        NSLog(@"_aFacts array is supposed to contain only NSString(s) and NSAttributedString(s). Instead a %@ has been found at index %d", NSStringFromClass(element.class), random);
    }
}

如您所见,此方法接收 UILabel 作为参数,并使用 _aFacts 中的随机值填充它。

3。调用 populateLabel

现在您需要更新填充标签的代码。

所以如果你有这样的东西:

self.label.text = [self.aFactBook randomAFact];

你需要用到这个:

[self.aFactBook populateLabel:self.label];

希望对您有所帮助。

关于ios - objective-c - 如何在数组中显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31772262/

相关文章:

iphone - 如何捕获在 iOS 调试器中按下的 "Stop"按钮?

ios - 如何从 OSX 终端将文件推送到未越狱的 iPad/iPhone?

ios - 使用 Core Data 存储自定义对象

c++ - 计算数组中特定行的平均值并存储在另一个数组中

ios - 限制 iOS 上某些设备的旋转 - Swift

ios - UILabel 没有响应触摸事件

objective-c - 打印指针地址和&符号地址之间的区别

PHP 从 mysql 结果设置为图表系列?

javascript - 我想从状态中删除的选定值数组未按预期工作 - React

ios - UIViewController 动画停止工作