簡體   English   中英

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

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

我有一篇我正在創作的新文章。 正在為文章標題添加UILabel 標題文本有時可能是一行,兩行或三行文本。 然后我需要在標題下面創建一個UILabel

由於標題具有動態高度,因此無法定位日期UILabel

是否有計算來獲得我的UILabel框架的底部位置?

注意:我使用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);

好吧,看起來你有一個錯字。 您再次調用[title sizeToFit]而不是[titleDate sizeToFit],這可能是問題的一部分。

假設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