是否可以在NSString之间插入Image? [英] 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.
截至目前,根据我的要求,我正在插入一个UIImage作为UILabel的子视图。但每当我需要计算产品名称长度时,我就会在其中定义UIImage的x位置并一次又一次地添加它。产品名称的大小可变。
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.
根本没有时候图像的x位置无法正确设置,因此它与UILabel上的文本重叠。我遇到了这个问题。
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];
推荐答案
你可以这样使用
label.text=[NSString stringWithFormat:@"%@ ⬆\uFE0E (%i%%)",product,level];
label.text=[NSString stringWithFormat:@"%@ ⬇\uFE0E (%i%%)",product,level];
只需将此箭头图像复制并粘贴到NSString。
just copy and paste this arrow image to NSString.
更改标签的foreColor将更改箭头颜色。
Change foreColor of label will change the arrow color.
从此处获取要粘贴的其他图像
get Other Images to paste from here
箭头 - 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
这篇关于是否可以在NSString之间插入Image?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!