0

I'm trying to show in my app also images instead of text and so I'm trying to integrate UIImages in my text array. Badly I get every time I try to implement an error so I decided to ask objective-c pros. Every help is needed and welcome!

This is my .h array code:

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

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

and my .m array code

-(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

This code is shown in a label. Do you think the label will show an successful integrated image in the array?

agr0
  • 33
  • 8
  • 1
    You can't put strings and images in an array and then hope to only get strings out. You need to organise your data better, like with a custom class. You also can't show images directly in a label. – Wain Aug 02 '15 at 13:59
  • @Wain no I want a mixed output. One time images and one time strings. Could you please help me? – agr0 Aug 02 '15 at 14:00
  • Why don't you use a `UIImageView` to show the image? – Luca Angeletti Aug 02 '15 at 14:01
  • @appzYourLife because this is just a fragment of my code and everything is configured for a label so changing would be really difficult. – agr0 Aug 02 '15 at 14:03
  • Ok. You can insert images inside a `UILabel` using the `attributedText` property. Look over here: http://stackoverflow.com/questions/19318421/how-to-embed-small-icon-in-uilabel – Luca Angeletti Aug 02 '15 at 14:05
  • @appzYourLife and do you have an idea how to do this with an array? Sorry but I'm a objective-c newbee – agr0 Aug 02 '15 at 14:07
  • Yes must you need to show mere where the method `randomFact` gets called to populate the label. We will need to update that piece of code too. – Luca Angeletti Aug 02 '15 at 14:14
  • @appzYourLife once in the viewDidLoad in my vc self.Label.text = [self.aFactBook randomAFact]; and then every time by clicking the new button self.Label.text = [self.aFactBook randomAFact]; – agr0 Aug 02 '15 at 14:17

1 Answers1

1

Preamble

To add text to a label you write this (as you are doing now).

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

To add an image you write this.

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

So we should do the following changes to your code.

1. Populating the array.

You should not add the image directly inside the array. Add an NSAttributedString instead. So your init becomes:

- (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. Updating randomAFact

Delete your randomFact method and add this new version.

- (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);
    }
}

As you can see this method receives as parameter a UILabel and populates it with a random value from _aFacts.

3. Calling populateLabel

Now you will need to update the code that populates your label.

So where you have something like this:

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

you will need to use this:

[self.aFactBook populateLabel:self.label];

Hope it helps.

Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/85014/discussion-on-answer-by-appzyourlife-objective-c-how-to-show-an-uimage-in-an-a). – Martijn Pieters Aug 03 '15 at 14:07