2013-04-22 37 views
1

我有一篇新文章,我正在創建。正在爲文章標題添加UILabel。標題文本有時可能是一行,兩行或三行文本。然後我需要在標題下創建一個UILabel找到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); 

回答

4

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

至於定位您的日期標籤假設10px的空間:

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

感謝。我也意識到我不應該試圖在我的標題上設置「中心」,而應該設置框架。 – 2013-04-23 17:20:55

+1

是啊,我只在使用中心時,我試圖對齊多個視圖的中心。 – JonahGabriel 2013-04-23 17:54:48