找到 UILabel 底部位置,以便可以在下面创建第二个 UILabel [英] Find UILabel bottom position so a second UILabel can be created below

查看:25
本文介绍了找到 UILabel 底部位置,以便可以在下面创建第二个 UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一篇新文章.正在为文章标题添加 UILabel.标题文本有时可能是一行、两行或三行文本.然后我需要在标题下方创建一个 UILabel.

I have a new article that I am creating. A UILabel is being added for the article title. 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?

注意:我使用 siteToFit 来确保 UILabel 的框架适合.

Note: I am using siteToFit to make sure the UILabel's frame fits.

// 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);

推荐答案

好吧,看来您打错了.您再次调用 [title sizeToFit] 而不是 [titleDate sizeToFit] 这可能是问题的一部分.

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.

至于定位日期标签假设 10px 空间:

As for positioning your date label assuming a 10px space:

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

这篇关于找到 UILabel 底部位置,以便可以在下面创建第二个 UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆