我正在尝试查看有关3种不同标签的信息; 第一。标题 二。发布日期 三。描述
标题和发布日期之间有两个空行。有时当标题在出版日期写入超过3行时。 这是一张图片(粗线是pub日期标签):
,有时当描述标签收到大量文字时会发生什么:
这应该是:
所以问题: 1.如何在标题和pubdate之间保存固定空间? (无论标题在两个固定空间之间保留多少行) 2.当他获得大量文本时,为什么描述会向下移动?我怎么能找到他的位置?
我只想在3个标签之间保存一个空格,无论每个标签有多少行。
到目前为止,这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
[_scroller setScrollEnabled:YES];
[_scroller setContentSize:CGSizeMake(320, 600)];
self.lblDescription.text = self.description;
self.lblTitle.text = self.stitle;
self.lblPubDate.text = self.pubDate;
self.lblTitle.numberOfLines = 0;
[self.lblTitle sizeToFit];
self.lblTitle.textAlignment = UITextAlignmentRight;
CGRect titleFram = self.lblTitle.frame;
titleFram = CGRectMake(titleFram.origin.x, titleFram.origin.y, 280, titleFram.size.height);
self.lblTitle.frame = titleFram;
self.lblDescription.numberOfLines = 0;
[self.lblDescription sizeToFit];
[self resizeHeightToFitForLabel:self.lblTitle];
[self resizeHeightToFitForLabel:self.lblDescription];
}
-(void)resizeHeightToFitForLabel:(UILabel *)label
{
CGRect newFrame = label.frame;
newFrame.size.height = [self heightForLabel:label withText:label.text];
label.frame = newFrame;
}
-(CGFloat)heightForLabel:(UILabel *)label withText:(NSString *)text
{
CGSize maximumLabelSize = CGSizeMake(290, FLT_MAX);
CGSize expectedLabelSize = [text sizeWithFont:label.font
constrainedToSize:maximumLabelSize
lineBreakMode:label.lineBreakMode];
return expectedLabelSize.height;
}
完成这项工作的最佳方法是什么? 感谢..
答案 0 :(得分:2)
你可以这样试试,
NSString *lbltext1 = lblTitle.text;
NSString *lbltext2 = lblDate.text;
NSString *lbltext3 = lblDesc.text;
CGRect rect = self.lblTitle.frame;
rect.size.height = [ViewController calculateHeightForText:lbltext1 font:self.lblTitle.font size:CGSizeMake(rect.size.width, 40000)].height;
self.lblTitle.frame = rect;
rect = self.lblDate.frame;
rect.origin.y = CGRectGetMaxY(self.lblTitle.frame) + 5;
rect.size.height = [ViewController calculateHeightForText:lbltext2 font:self.lblDate.font size:CGSizeMake(rect.size.width, 40000)].height;
self.lblDate.frame = rect;
rect = self.lblDesc.frame;
rect.origin.y = CGRectGetMaxY(self.lblDate.frame) + 5;
rect.size.height = [ViewController calculateHeightForText:lbltext3 font:self.lblDesc.font size:CGSizeMake(rect.size.width, 40000)].height;
self.lblDesc.frame = rect;
用于计算身高,
+ (CGSize)calculateHeightForText:(NSString *)text font:(UIFont *)font size:(CGSize)size {
CGSize _size = [text sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
return _size;
}
答案 1 :(得分:1)
试试这个,
- (void)viewDidLoad
{
[super viewDidLoad];
[_scroller setScrollEnabled:YES];
[_scroller setContentSize:CGSizeMake(320, 600)];
//Use this way
[self positionLabel:self.lblTitle withText:self.stitle withY:10];
[self positionLabel:self.lblPubDate withText:self.pubDate withY:CGRectGetMaxY(self.lblTitle.frame)+10 ];
[self positionLabel:self.lblDescription withText:self.description withY:CGRectGetMaxY(SELF.lblPubDate.frame)+10 ];
}
-(void)positionLabel:(UILabel*)lbl withText:(NSString*)text withY:(CGFloat)Y
{
lbl.textAlignment = UITextAlignmentRight;
lbl.text = text;
lbl.numberOfLines = 0;
lbl.lineBreakMode = NSLineBreakByWordWrapping;
CGSize size = [self calculateSize:lbl];
//FIXED
[lbl setFrame:CGRectMake(10 , Y , (size.width>310)?size.width:310 , size.height)];
}
-(CGSize)calculateSize:(UILabel*)lbl
{
CGSize size = [lbl.text sizeWithFont:lbl.font
constrainedToSize:CGSizeMake(310, MAXFLOAT)
lineBreakMode:UILineBreakModeWordWrap];
return size;
}
答案 2 :(得分:0)
使用我的波纹管自定义方法计算其内容高度...
(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width :(UILineBreakMode)lineBreakMode
{
[text retain];
[withFont retain];
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];
[text release];
[withFont release];
return suggestedSize.height;
}
并像下面那样使用它。
UILabel *lblAddress = [[UILabel alloc]init];
[lblAddress setFrame:CGRectMake(110, 31, 200, 50)];
lblAddress.text = @"your Text ";
lblAddress.lineBreakMode = UILineBreakModeWordWrap;
lblAddress.numberOfLines = 0;
lblAddress.font = [UIFont fontWithName:@"Helvetica" size:12];
lblAddress.frame = CGRectMake(lblAddress.frame.origin.x, lblAddress.frame.origin.y,
200,[self calculateHeightOfTextFromWidth:lblAddress.text :lblAddress.font :200 :UILineBreakModeWordWrap] );
lblAddress.textColor = [UIColor darkGrayColor];
[self.view addSubview:lblAddress];
见My Blog ...
答案 3 :(得分:0)
试试这个
self.lblDescription.text = @"asdfasfadfsa";
self.lblTitle.text = @"asdafsasf";
self.lblPubDate.text = @"asdafsas";
self.lblTitle.numberOfLines = 0;
[self.lblTitle sizeToFit];
self.lblTitle.textAlignment = UITextAlignmentRight;
CGRect titleFram = self.lblTitle.frame;
titleFram = CGRectMake(titleFram.origin.x, titleFram.origin.y, 280, titleFram.size.height);
self.lblTitle.frame = titleFram;
self.lblDescription.numberOfLines = 0;
[self.lblDescription sizeToFit];
[self resizeHeightToFitForLabel:self.lblTitle];
CGRect pubFrame =self.lblPubDate.frame;
pubFrame.origin.y= self.lblTitle.frame.origin.y+self.lblTitle.frame.size.height+110;
[self.lblPubDate setFrame:pubFrame];
CGRect descFrame =self.lblDescription.frame;
descFrame.origin.y= self.lblPubDate.frame.origin.y+self.lblPubDate.frame.size.height+30;
[self resizeHeightToFitForLabel:self.lblDescription];