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];
user2125861
source share