2014-12-23 3 views
0

자동 레이아웃을 사용 중입니다. 맞춤 설정에서 UITableViewCell은 최대 너비가 UILabel입니다. 나는 텍스트가 적다면 많은 방법을 시도했다. 줄이거 나 텍스트가 많으면 레이블의 높이가 높아집니다.UITableViewCell의 UILabel이 자동 레이아웃으로 크기가 조정되지 않습니다?

나는 UILabelCellForRowAtIndex에 만들려고했는데, 그 이유는 분명한 이유가있다. 그런 다음 글꼴 및 레이블의 도움으로 CGSize을 얻으려고했습니다. 그것의 축소하지만 그 적절하고 구현을위한 나는 다시 제약을 그려야 해. 기본적으로 내가 시도한 솔루션은 시각적으로 매력적이지 않습니다. 제발 도와주세요. 나는

감사 enter image description here

+0

당신이 제약의 어떤 종류가 있습니까를 화면의 현재 상태의 사진을 첨부? 그 라벨의 부모보기는 무엇입니까? 여러 줄로 된 텍스트입니까? –

+0

@ JulianKról : 'UITableViewCell'이 (가) 상위입니다. 'UILabel'의 numberoflines 속성보기가 0입니다. 나는 $ 꼬리, 꼬리, 위, 아래 공간을 포함하고있다. – coreDeviOS

답변

1
//Array of name 
NSMutableArray *nameArray; 

//take outlet of height constraint in custom cell; 
@property(weak) IBOutlet NSLayoutConstraint *labelHeightConstraint; 


-(void)viewDidLoad 
{ 
_nameArray=[NSMutableArray arrayWithObjects:,@"I am using Auto-layout. In Custom  UITableViewCell added a UILabel with max width.",@"Hi I am Rohit",@"I Like To Help Society" ,nil]; 
} 


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
CustomerRatingsCell *customerRatingsCell=[tableView dequeueReusableCellWithIdentifier:@"CustomerRatingsIdentifier"]; 
customerRatingsCell.labelName=[_nameArray objectAtIndex:indexPath.row]; 
customerRatingsCell.headerLabelConstraint=[self getLabelHeight:[NSString stringWithFormat:@"%@",[_nameArray objectAtIndex:indexPath.row]]]; 
return CustomerRatingsCell; 

} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

     return [self getLabelHeight:[NSString stringWithFormat:@"%@",[_nameArray objectAtIndex:indexPath.row]]]+25 ;//plus 25 is remaining space apart from label height, if you are using the full height label equals to cell height then don't add it. 

} 



- (CGFloat)getLabelHeight:(NSString*)textLabel 
{ 
    CGRect textRect = [textLabel boundingRectWithSize:CGSizeMake(300, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont fontWithName:Avenir Roman size:12.0f]} context:nil]; 

    CGSize expectedLabelSize = CGSizeMake(textRect.size.width, textRect.size.height); 

    return expectedLabelSize.height; 
} 
+1

다시 한번 의견을 말하고 코드 섹션에 넣어 주시겠습니까? – coreDeviOS

+0

나는 높이를 바꾸고 싶지 않다 .. 너비? – coreDeviOS

+0

질문을 받고 있지 않은 스크린 샷을 보내주십시오. –

관련 문제