Is it possible to insert an image between NSString?

I am working on a product application in which it shows the name of the product with a price agreement or decree.

Currently, as per my requirement, I am embedding UIImage as a sub-view in UILabel. But each time I need to calculate the length of the product name on which I determine the x position of the UIImage and add it again and again. The product name is variable in size.

It’s not always that the x position of the image cannot be set properly, so it overlaps the text on UILabel. I am stuck in this problem.

The following are my efforts to address this requirement. Maybe there should be another way to do such things, but I don’t know. Any other alternative that 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]; 
+4
source share
3 answers

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 into NSString.

Changing the foreColor label will change the color of the arrow.

get other images to insert from here

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

Circular Numbers - 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

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

+5
source

You can use Unicode characters for arrows (⬈ and ⬊) with NSAttributedStrings for colors or even Emojis: β†— and β†˜ (view this page in the simulator to see them).

+1
source

The easiest way to achieve this is to use a UIWebView and insert your image as an HTML tag. But in terms of performance, this is not great.

-1
source

Source: https://habr.com/ru/post/1489258/


All Articles