如何在动态标签后添加图像?

时间:2013-06-21 06:48:41

标签: iphone ios objective-c

我有一个应用程序,我在标签后添加图像,标签是动态的。我需要在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];

但对某些人来说这是正确的,但另一方面则是错误的。任何人都可以指导我哪里出错了吗?

5 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

当你创建UILabel时,每当你获得标签的大小时,你也会得到 UILabel的'X','Y'和'Height'。创建UILabel后添加图像很有帮助。 您还需要添加动态UIImageView作为UILabel以添加图片。

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

你的UILabel sise是,

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 Fram给出了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)

答案 2 :(得分:1)

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

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

希望这会对你有所帮助。

答案 3 :(得分: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];

答案 4 :(得分: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];