2016-04-09 39 views
3

我正在寫一個可擴展的tableView標題,我需要在tableView委託方法中準確設置標題的高度。現在,我使用下面的方法來計算我的頭的高度(多行標籤):如何通過行間距獲得UILabel的完全高度?

CGRect calcuRect = [headerText boundingRectWithSize:CGSizeMake(myLabelWitdh, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:myLabelFont} context:nil]; 
CGFloat headerHeight = calcuRect.size.height; 

然而,我發現,計算出的高度幾乎沒有文本包括行空間。 那麼如何獲得標籤的行距?或者如何用線空間獲得UILabel的精確高度?

+0

有人能理解我嗎?如何獲得value = wordsTotalHeight +(lineNum-1)* lineSpace。此方法返回單詞TotalHeight,但如何獲得lineSpace的值? – ZyusAn

回答

4

你可以得到的UILabel的確切高度恰好與路過相同的字體大小和類型,並做一些計算。
在這裏,我使用Helvetica字體一個UILabel與字體大小16.

目標C

- (CGFloat)requiredHeight:(NSString*)labelText{ 

    UIFont *font = [UIFont fontWithName:@"Helvetica" size:16.0]; 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, CGFLOAT_MAX)]; 
    label.numberOfLines = 0; 
    label.lineBreakMode = NSLineBreakByWordWrapping; 
    label.font = font; 
    label.text = labelText; 
    [label sizeToFit]; 
    return label.frame.size.height; 

} 

輸出

CGFloat size = [self requiredHeight:@"iOS Rocks"]; 
NSLog(@"%f",size); 

size = [self requiredHeight:@"iOS Rocks\n"]; 
NSLog(@"%f",size); 

控制檯輸出

2016-04-10 01:37:46.812 testPro[6093:327503] 18.500000 
2016-04-10 01:37:46.814 testPro[6093:327503] 37.000000 

雨燕2.2

func requiredHeight(labelText:String) -> CGFloat { 

    let font = UIFont(name: "Helvetica", size: 16.0) 
    let label:UILabel = UILabel(frame: CGRectMake(0, 0, 200, CGFloat.max)) 
    label.numberOfLines = 0 
    label.lineBreakMode = NSLineBreakMode.ByWordWrapping 
    label.font = font 
    label.text = labelText 
    label.sizeToFit() 
    return label.frame.height 

} 

編輯
雨燕3.0

func requiredHeight(labelText:String) -> CGFloat { 

    let font = UIFont(name: "Helvetica", size: 16.0) 
    let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: .max)) 
    label.numberOfLines = 0 
    label.lineBreakMode = .byWordWrapping 
    label.font = font 
    label.text = labelText 
    label.sizeToFit() 
    return label.frame.height 

} 
+0

@ZyusAn它對你有用嗎? –

+0

謝謝,真的有幫助 – ZyusAn

0

您需要調整標籤框的大小。試試:[label sizeToFit];

+0

不是關於sizeToFit,我只是想知道多標籤 – ZyusAn

0

以下使用方法相同。

- (CGFloat)heightForWidth:(CGFloat)width usingFont:(UIFont *)font withText:(NSString *)aStrText 
{ 
    CGSize labelSize = (CGSize){width, FLT_MAX}; 
    CGRect rect = [aStrText boundingRectWithSize:labelSize 
             options:NSStringDrawingUsesLineFragmentOrigin 
             attributes:@{NSFontAttributeName : font} 
             context:nil]; 
    return rect.size.height; 
} 
+0

問題的確切高度,這種方法只返回高度的話,不包括行間距,如果標籤有多行,這個高度會更小比實際高度。 – ZyusAn

0
override func viewDidLayoutSubviews() { 
      super.viewDidLayoutSubviews() 
      sizeToFit() 

     } 
     func sizeToFit(){ 
      let header = customHeaderView 
      let height = header.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height; 
      var headerFrame = header.frame 

      headerFrame.size.height = height 
      header.frame = headerFrame 
      tableView.tableHeaderView = header; 



    } 
Hope this will help you. 
0

其實,我想在我的表視圖創建可擴展節頭,但是在委託方法:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 

this met hod必須返回我的標籤標題的正確高度,我使用下面的方法來計算高度,但高度不包含行間距,但僅包含單詞高度;

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary<NSString *, id> *)attributes context:(NSStringDrawingContext *)context; 

由於更多地瞭解sizeToFit,這裏是一個可行的答案

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [self tableViewInit]; 
    [self headerInit]; 
} 

- (void)tableViewInit { 
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds]; 
    [self.view addSubview:_tableView]; 
    self.tableView.dataSource = self; 
    self.tableView.delegate = self; 
} 

- (void)headerInit { 
    CGRect originSize = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, CGFLOAT_MIN); 
    self.header = [[UILabel alloc] initWithFrame:originSize]; 
    self.header.lineBreakMode = NSLineBreakByWordWrapping | NSLineBreakByTruncatingTail; 
    self.header.text = self.headerStr; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
    if (self.showAll) { 
     self.header.numberOfLines = 0; 
     [self.header sizeToFit]; 
    } else { 
     self.header.numberOfLines = 2; 
     [self.header sizeToFit]; 
    } 
    return self.header.bounds.size.height; 
} 

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    return self.header; 
} 
0

基於拉詹Maheshwari答案,我在雨燕3.0寫了這個功能,使用可選的行高度通用的標籤。希望能幫助到你。

func heightForLabel(_ text: String, font: UIFont, width: CGFloat, lineSpacing: CGFloat) -> CGFloat { 
    let label = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude)) 
    label.numberOfLines = 0 
    label.lineBreakMode = NSLineBreakMode.byWordWrapping 
    label.font = font 
    if lineSpacing > 0.0 { 
     let style = NSMutableParagraphStyle() 
     style.lineSpacing = lineSpacing 
     style.alignment = .center 
     label.attributedText = NSAttributedString(string: text, attributes: [NSParagraphStyleAttributeName: style]) 
    } else { 
     label.text = text 
    } 
    label.sizeToFit() 
    return label.frame.height 
}