Reputation: 8947
I had an application in which i am adding an image after the label,the label is dynamic. I need to add an image after that UILabel
. I tried `
dropdownlabel = [[UILabel alloc]init];
[dropdownlabel setFrame:CGRectMake(90,8,180, 30)];
[dropdownlabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
dropdownlabel.textAlignment = UITextAlignmentLeft;
dropdownlabel.textColor = [UIColor whiteColor];
dropdownlabel.numberOfLines = 0;
dropdownlabel.text=@"gdhhgsfghdfagsfd ";
dropdownlabel.backgroundColor =[UIColor clearColor];
[navview addSubview:dropdownlabel];
CGSize maximumLabelSize = CGSizeMake(9999,30);
CGSize expectedLabelSize = [dropdownlabel.text sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]constrainedToSize:maximumLabelSize lineBreakMode:dropdownlabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = dropdownlabel.frame;
newFrame.size.height = expectedLabelSize.height;
dropdownlabel.frame = newFrame;
NSLog(@"%f",newFrame.size.width);
UIImageView *navview1=[[UIImageView alloc] initWithFrame:CGRectMake( newFrame.size.width+15,13,20,20)];
navview1.image = [UIImage imageNamed:@"down_sml_arrow.png"];
navview1.userInteractionEnabled=YES;
[navview addSubview:navview1];
But for some it is correct, but for another it is wrong. Can anybody guide me where i am going wrong?
Upvotes: 0
Views: 457
Reputation: 47059
WhenEver Your UILabel
Is created Each Time you got its Size of your label, So you also Got UILabel's 'X', 'Y' and 'Height'. It is Helpful for Add Image after Created UILabel
.
YOu also need to Add Dynamic UIImageView
as UILabel
for add Images.
How To give Fram of UIImageView
after Added UILabel
, For Example.
your UILabel
sise is,
myLabel.fram = CGRectMake(10, 290, 200, 50);
MyLabel.fram.origin.x = 10;
MyLabel.fram.origin.y = 290;
MyLabel.fram.size.width = 200;
MyLabel.fram.size.height = 50;
so, I give fram of UIImageView
base on myLabel's
Fram;
myImageView.fram = CGRectMake(MyLabel.fram.origin.x, MyLabel.fram.origin.y + MyLabel.fram.size.height , 200, 50); /// Here you can also set width and height as you need (similar to UILabel also)
Upvotes: 1
Reputation: 1233
You're just taking the width of the labels frame as your origin.x
value for your imageView. Instead you also have to consider the origin value of your label. User CGRectGetMaxX(newFrame) + 15
for the origin of your UIImageView
.
Upvotes: 1
Reputation: 1645
dropdownlabel = [[UILabel alloc]init];
[dropdownlabel setFrame:CGRectMake(90,8,180, 30)];
[dropdownlabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
dropdownlabel.textAlignment = NSTextAlignmentRight;
dropdownlabel.textColor = [UIColor whiteColor];
dropdownlabel.numberOfLines = 0;
dropdownlabel.text=@"gdhhgsfghdfagsfd ";
dropdownlabel.backgroundColor =[UIColor clearColor];
[navview addSubview:dropdownlabel];
CGSize expectedLabelSize = [dropdownlabel.text sizeWithFont:dropdownlabel.font];
//adjust the label the the new height.
CGRect newFrame = dropdownlabel.frame;
newFrame.size = expectedLabelSize;
dropdownlabel.frame = newFrame;
UIImageView *navview1=[[UIImageView alloc] initWithFrame:CGRectMake(newFrame.origin.x+newFrame.size.width+15,13,20,20)];
navview1.image = [UIImage imageNamed:@"down_sml_arrow.png"];
navview1.userInteractionEnabled=YES;
[navview addSubview:navview1];
Upvotes: 1
Reputation: 19418
You should also consider label's origin X with width
UIImageView *navview1=[[UIImageView alloc] initWithFrame:CGRectMake(newFrame.origin.x+newFrame.size.width+5,13,20,20)];
navview1.image = [UIImage imageNamed:@"down_sml_arrow.png"];
navview1.userInteractionEnabled=YES;
[navview addSubview:navview1];
Upvotes: 1
Reputation: 2251
I am assuming that you are changing the width of the label dynamically.
UIImageView *navview1=[[UIImageView alloc] initWithFrame:CGRectMake(dropdownlabel.frame.origin.x+dropdownlabel.frame.size.width+15 ,13,20,20)];
Hope this will help you.
Upvotes: 1