[英]Header/Footer height of a UITableView section based on title text height
我想知道是否有可能使UITableView的某個部分的高度頁眉/頁腳等於頁眉/頁腳標題文本的高度。 任何提示都很棒!
注意:我的TableView的某些部分可能沒有頁眉/頁腳,在這種情況下,僅在部分之間具有填充,因為在這種情況下,“ headerTitleHeight / footerTitleHeight”將為零。
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return headerTitleHeight + padding;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return footerTitleHeight + padding;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
NSString *myHeader = [sectionsArray objectAtIndex:section];
CGSize maxSize = CGSizeMake(320, 999999.0);
int height = 0;
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
font, NSFontAttributeName,
nil];
if (IS_IOS_6) //Macro i made for if iOS 6
{
CGSize size = [myHeader sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:(15)]
constrainedToSize:maxSize
lineBreakMode:NSLineBreakByWordWrapping];
height = size.height;
}
else // IOS 7 , Attributes
{
CGRect frame = [myHeader boundingRectWithSize:maxSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributesDictionary
context:nil];
height = frame.size.height;
}
return height+5;
}
你可以
return UITableViewAutomaticDimension;
編輯:糟糕,忽略以下內容,請參閱下面的評論。
要么
return UITableViewAutomaticDimension + padding;
您可以自動設置尺寸,但必須具有估計的尺寸。 它對Footer和Header的工作方式相同。
func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat {
return 300
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return UITableViewAutomaticDimension
}
計算文本大小並添加填充高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
CGFloat headerTitleHeight = [jobTitleString sizeWithFont:[UIFont fontWithName:@"OpenSans-Light" size:20.0f] constrainedToSize:CGSizeMake(width,9999)].height;
return headerTitleHeight + padding;
}
您可以使用boundingRectWithSize方法獲取為頁眉/頁腳繪制特定NSString所需的高度,然后在UITableView委托方法中返回值。 對於沒有頁眉頁腳的部分,您必須編寫代碼以僅返回填充。
您可以嘗試以下方法。
CGSize constraint = CGSizeMake(<header/footer width>, <max header/footer height>);
CGSize size;
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
NSRange range = NSMakeRange(0, [<string> length]);
NSDictionary *attributes = [<string> attributesAtIndex:0 effectiveRange:&range];
CGSize boundingBox = [<string> boundingRectWithSize:constraint options: NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
}
else
{
size = [<string> sizeWithFont:descriptionLabel.font constrainedToSize:constraint lineBreakMode:descriptionLabel.lineBreakMode];
}
return size.height + padding;
要么
CGSize maximumLabelSize = CGSizeMake(<header/footer width>, <max header/footer height>);
CGSize expectedSize = [<string label> sizeThatFits:maximumLabelSize];
return expectedSize.height + padding;
在viewDidLoad
, heightForFooterInSection
和heightForHeaderInSection
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.estimatedSectionFooterHeight = 50; // for footer
self.tableView.estimatedSectionHeaderHeight = 50; // for header
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return UITableViewAutomaticDimension;
}
在頁腳/頁眉xib文件中
-將UILabel
頂部和底部約束都添加到ContainerView
中
-添加UILabel約束高度,然后從Equal
-> Greater than or Equals
更改UILabel約束高度
-最后將UILabel行更改為0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.