简体   繁体   English

找到UILabel底部位置,以便在下面创建第二个UILabel

[英]Find UILabel bottom position so a second UILabel can be created below

I have a new article that I am creating. 我有一篇我正在创作的新文章。 A UILabel is being added for the article title. 正在为文章标题添加UILabel The title text sometimes might be one, two or three lines of text. 标题文本有时可能是一行,两行或三行文本。 I then need to create a UILabel below the title. 然后我需要在标题下面创建一个UILabel

I am having trouble positioning the date UILabel since the title has a dynamic height. 由于标题具有动态高度,因此无法定位日期UILabel

Is there a calculation to get the bottom position of my UILabel 's frame? 是否有计算来获得我的UILabel框架的底部位置?

Note: I am using siteToFit to make sure the UILabel 's frame fits. 注意:我使用siteToFit来确保UILabel的框架适合。

// Text
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.view.bounds.size.width-20, 50)];
    //title.backgroundColor = [UIColor clearColor];
    title.backgroundColor = [UIColor redColor];
    title.text = self.firstItem.title;
    //title.text = @"This is a test of a short title that is a little longer";
    title.textColor = [UIColor whiteColor];
    title.layer.shadowColor = [UIColor blackColor].CGColor;
    title.layer.shadowOffset = CGSizeMake(0, 0);
    title.layer.shadowRadius = 0.6;
    title.textAlignment = NSTextAlignmentLeft;
    title.layer.shadowOpacity = 1.0;
    title.layer.masksToBounds = NO;
    title.font = [UIFont boldSystemFontOfSize:16.0];
    title.numberOfLines = 0;
    [title sizeToFit];

    [self.firstNewsItemBackgroundView addSubview:title];
    self.cachedParalax = self.firstNewsItemBackgroundView.frame;

    // Fix positioning
    title.center = CGPointMake(title.center.x, self.imageView.frame.size.height - title.frame.size.height);

    // Date
    UILabel *titleDate = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.view.bounds.size.width-20, 50)];
    titleDate.backgroundColor = [UIColor clearColor];
    titleDate.text = self.firstItem.publishedDateString;
    titleDate.textColor = [UIColor whiteColor];
    titleDate.layer.shadowColor = [UIColor blackColor].CGColor;
    titleDate.layer.shadowOffset = CGSizeMake(0, 0);
    titleDate.layer.shadowRadius = 0.7;
    titleDate.textAlignment = NSTextAlignmentLeft;
    titleDate.layer.shadowOpacity = 0.95;
    titleDate.layer.masksToBounds = NO;
    titleDate.font = [UIFont italicSystemFontOfSize:12.0];
    titleDate.numberOfLines = 0;
    [title sizeToFit];
    [self.firstNewsItemBackgroundView addSubview:titleDate];

    // Fix positioning
    CGRect frame = titleDate.frame;
    titleDate.frame = CGRectMake(10, title.center.y + (title.frame.size.height/2), frame.size.width, frame.size.height);

Well, it looks like you have a typo. 好吧,看起来你有一个错字。 You are calling [title sizeToFit] again instead of [titleDate sizeToFit] which may be part of the problem. 您再次调用[title sizeToFit]而不是[titleDate sizeToFit],这可能是问题的一部分。

As for positioning your date label assuming a 10px space: 假设10px空间,请定位日期标签:

CGRect dateFrame = titleDate.frame;
dateFrame.origin.y = title.frame.origin.y + title.frame.size.height + 10; 
[titleDate setFrame:dateFrame]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM