0

我有一个应用程序,我在标签后添加图像,标签是动态的。之后我需要添加一张图片UILabel。我试过`

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

但对一些人来说是正确的,但对另一些人来说是错误的。谁能指导我哪里出错了?

4

5 回答 5

1

您只是将标签框架的宽度作为您origin.x的 imageView 的值。相反,您还必须考虑标签的原始值。用户CGRectGetMaxX(newFrame) + 15 为您的UIImageView.

于 2013-06-21T06:52:27.993 回答
1

WhenEver Your UILabelIs created 每次你得到标签的大小,所以你也得到了UILabel 的 'X', 'Y' 和 'Height'。创建后添加图像很有帮助UILabel。您还需要UIImageViewUILabel添加图像一样添加动态。

如何UIImageView在添加后给出 Fram UILabel,例如。

你的UILabel问题是,

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;

所以,我给出了UIImageView基于myLabel'sFram 的框架;

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)
于 2013-06-21T06:53:50.823 回答
1

我假设您正在动态更改标签的宽度。

 UIImageView *navview1=[[UIImageView alloc] initWithFrame:CGRectMake(dropdownlabel.frame.origin.x+dropdownlabel.frame.size.width+15 ,13,20,20)];

希望这会帮助你。

于 2013-06-21T06:56:14.127 回答
1

您还应该考虑标签的原点 X 和宽度

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];
于 2013-06-21T06:58:37.173 回答
1
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];
于 2013-06-21T06:59:53.410 回答