프로그램/iOS

iOS 동적 라벨에 따라서 cell 높이 변경

잡식성초보 2016. 2. 19. 17:34

Array 속 Dictionary 에서 라벨을 읽어와 라벨의 최소 최대 크기를 주고 다시 Dictionary에 setObject하여 cell 높이 값을 준다.


TableCell.m 파일에서


static UILabel* eLabel = nil ;


+ (CGSize)SizeWithDic:(NSDictionary *) dic {

    //폰트 크기 및 사이즈 정렬 라인을 0으로 해줘야한다.

    ifeLabel == nil ) {

        eLabel = [UILabel new] ;

        eLabel.font = [UIFont systemFontOfSize:12.0f weight:UIFontWeightRegular] ;

        eLabel.lineBreakMode = NSLineBreakByCharWrapping ;

        eLabel.numberOfLines = 0 ;

    }

    

    eLabel.text = [dic objectForKey:@"abc"] ;

    float width = [UIScreen mainScreen].bounds.size.width - 160;

    

    CGSize size = [eLabel sizeThatFits:CGSizeMake(width, 14.f)] ;

    size.height += 47.0f ;

    size.width += 160.0f ;

    

    return size ;

}


TableView에서 사용한다


NSMutableDictionary* Dic = [[NSMutableDictionary alloc]initWithDictionary:[self.Array objectAtIndex:i]];

                     CGSize test = [ListViewCell SizeWithDic: Dic];

                     NSValue *test2 = [NSValue valueWithCGSize:test];

                     [Dic setObject:test2 forKey:@"ABC"];

                     [self.Array replaceObjectAtIndex:i withObject: Dic];

반응형

'프로그램 > iOS' 카테고리의 다른 글

iOS 코드로 버튼에 이벤트 주기  (0) 2016.02.26
iOS 테이블뷰 사용법  (0) 2016.02.22
iOs UILabel, UIButton 색상, 폰트, 글자체 변경  (0) 2016.02.19
iOS UIalertView 사용법  (0) 2016.02.19
iOS 동적 Uiview 생성하기  (0) 2016.02.19