是否可以在NSString之间插入Image?

4 iphone nsstring uiimage

我正在开发一个基于产品的应用程序,其中显示产品名称以及价格协议或法令.

截至目前,根据我的要求,我将UIImage作为子视图插入到UILabel.但是每当我需要计算产品名称长度时,我就会定义UIImage的x位置并一次又一次地添加它.产品名称是变量的尺寸.

根本没有时候图像的x位置无法正确设置,因此它与UILabel上的文本重叠.我遇到了这个问题.

以下是我对这种要求的努力.可能应该有另一种方法来做这些事情,但我不知道.我可以申请其他任何替代方案吗?

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];
Run Code Online (Sandbox Code Playgroud)

War*_*olf 5

你可以这样使用

label.text=[NSString stringWithFormat:@"%@ ?\uFE0E (%i%%)",product,level];
label.text=[NSString stringWithFormat:@"%@ ?\uFE0E (%i%%)",product,level];
Run Code Online (Sandbox Code Playgroud)

只需将此箭头图像复制并粘贴到NSString即可.

更改标签的foreColor将更改箭头颜色.

从这里获取其他图像粘贴

箭头 - http://www.alanwood.net/unicode/miscellaneous_symbols_and_arrows.html

圆形数字 - http://www.alanwood.net/unicode/enclosed_alphanumerics.html

图像 - http://www.alanwood.net/unicode/miscellaneous_symbols.html

微笑 - http://www.alanwood.net/unicode/emoticons.html

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