나름 프로그래밍?/ Objective-C

UITextView 글자 내용 길이에 맞추기 동적 높이 구하기

-Dong- 2010. 12. 27. 23:39
CGRect frame = _textView.frame;
frame
.size.height = _textView.contentSize.height;
_textView
.frame = frame;
Edit: sizeThatFits returns the size but does not actually resize the component. I'm not sure if that's what you want, or if [textView sizeToFit] is more what you were looking for. In either case, I do not know if it will perfectly fit the content like you want, but it's the first thing to try.

- http://stackoverflow.com/questions/50467/how-do-i-size-a-uitextview-to-its-content


ps > 테이블 생성시 높이 지정을 위해서 UITextView 생성을 하고 높이를 구하려 할때는 cellForRowAtIndexPath 에서 셀 생성 하듯이 하나 만들어서... (heightForRowAtIndexPath 에 임의로 실제 사용하는 셀을 생성해서 적용해보고 구해오기)
ps2 > sizeToFit 날리기 전에 사이즈 항상 늘려놓기(셀 재사용으로 인해 전에 설정이 이후에 영향을 미침)


위 방법은 테이블이 많이 길어지면 느려서..

CGSize maximumSize = CGSizeMake(300, 9999);
NSString *dateString = @"The date today is January 1st, 1999";
UIFont *dateFont = [UIFont fontWithName:@"Helvetica" size:14];
CGSize dateStringSize = [dateString sizeWithFont:dateFont
        constrainedToSize
:maximumSize
        lineBreakMode
:self.dateLabel.lineBreakMode];

CGRect dateFrame = CGRectMake(10, 10, 300, dateStringSize.height);

self
.dateLabel.frame = dateFrame;

이렇게도 가능