Is it possible to insert Image between NSString?

折月煮酒 提交于 2019-12-06 04:43:13

问题


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];

回答1:


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




回答2:


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).




回答3:


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.



来源:https://stackoverflow.com/questions/17423788/is-it-possible-to-insert-image-between-nsstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!