user2125861
user2125861

Reputation:

Is it possible to insert Image between NSString?

I am working on a product based application in which it shows product name with price increement or decreement.

As of now, according to my requirement I am inserting a UIImage as a subview to UILabel.But its everytime I need to calculate product name length on which I am defining x position of UIImage and adding it again and again.Product name is of variable size.

Not at all sometimes x position of image could not set properly so it overlaps text on UILabel.I stucked with this problem.

Below is my effort fired on such requirement.May be there should be another way to do such things but I don't know.Any other alternative I can apply?

int level=3;
NSString *product=@"Pea Price:";

UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 25)];
[imgView setImage:[UIImage imageNamed:@"up.png"]];

CGRect rect=[imgView frame];

UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(20, 70, 200, 30)];
label.backgroundColor=[UIColor whiteColor];

CGSize size=[product sizeWithFont:label.font constrainedToSize:CGSizeMake(MAXFLOAT, 30) lineBreakMode:NSLineBreakByTruncatingTail];
rect.origin.x=size.width-5;
[imgView setFrame:rect];

label.text=[NSString stringWithFormat:@"%@  (%i%%)",product,level];
[label addSubview:imgView];
[self.view addSubview:label];

Upvotes: 5

Views: 1227

Answers (3)

Prince Kumar Sharma
Prince Kumar Sharma

Reputation: 12641

You can use like that

label.text=[NSString stringWithFormat:@"%@ ⬆\uFE0E (%i%%)",product,level];
label.text=[NSString stringWithFormat:@"%@ ⬇\uFE0E (%i%%)",product,level];

just copy and paste this arrow image to NSString.

Change foreColor of label will change the arrow color.

get Other Images to paste from here

Arrows -- http://www.alanwood.net/unicode/miscellaneous_symbols_and_arrows.html

Circular Digits -- http://www.alanwood.net/unicode/enclosed_alphanumerics.html

Images -- http://www.alanwood.net/unicode/miscellaneous_symbols.html

Smiles -- http://www.alanwood.net/unicode/emoticons.html

Miscallaneous -- http://www.alanwood.net/unicode/miscellaneous-symbols-and-pictographs.html

Upvotes: 6

Cyrille
Cyrille

Reputation: 25144

You could use Unicode characters for arrows (⬈ and ⬊) with NSAttributedStrings for colors, or even Emojis : ↗ and ↘ (view this page in the Simulator to see them).

Upvotes: 1

Cyrille
Cyrille

Reputation: 25144

The simplest way to achieve this is to use a UIWebView and insert your image as an HTML tag. But performance-wise, it's not great.

Upvotes: -1

Related Questions