2011-01-11 4 views
7

sizeWithFont : constrainedToSize에 대한 엣지 케이스를 찾았습니다. 망막 디스플레이에서 가끔 한 줄의 높이가 1 줄을 반환합니다. 실제로 필요한 것보다 더 키가 크고 더 중요하게 실제로 끌리는 것보다 큽니다.NSString sizeWithFont : constrainedToSize : 망막 디스플레이에서 잘못된 높이를 반환 함

참고 : 사용중인 실제 코드는 성능 중심의 손으로 그려진 가변 높이 테이블 뷰 셀 코드 안에 묻혀 있으므로 가능한 한 간단한 샘플 코드로 문제를 추출했습니다. (제 질문 이외의 다른 답변을 시도 할 때 참고하십시오 :

이 샘플 UIView는 내용을 채우고 (줄 바꿈) 맞게 텍스트를 측정 한 다음 해당 rect를 채운 다음 텍스트를 그립니다.

망막 장치 (또는 시뮬레이터)에서는 높이가 1 줄로 너무 높지만 망막 이전 장치 (또는 시뮬레이터)에서는 올바른 높이를 반환합니다.

버그 수정이 필요하므로 누구나 가지고있는 통찰력에 대해 크게 감사드립니다!

많은 감사!

-Eric

- (void)drawRect:(CGRect)rect { 
NSString * theString = @"Lorem ipsum dolor sit ameyyet, consectetur adipiscing elit. Etiam vel justo leo. Curabitur porta, elit vel."; 
UIFont * theFont = [UIFont systemFontOfSize:12]; 
CGSize theConstraint = CGSizeMake(rect.size.width - 20, rect.size.height - 20); 
CGSize theResultSize = [theString sizeWithFont:theFont constrainedToSize:theConstraint]; 

// dump the measurements 
NSLog(@"returned a size h = %f, w = %f", theResultSize.height, theResultSize.width); 

// fill the whole rect 
CGContextRef context = UIGraphicsGetCurrentContext(); 
[[UIColor yellowColor] set]; 
CGContextFillRect(context, rect); 

// fill the measured rect 
CGRect theRect = CGRectMake(10, 10, theResultSize.width, theResultSize.height); 
context = UIGraphicsGetCurrentContext(); 
[[UIColor cyanColor] set]; 
CGContextFillRect(context, theRect); 

// draw the text 
[[UIColor blackColor] set]; 
[theString drawInRect:theRect withFont:theFont]; 
} 

전체 간단한 프로젝트는 here 사용할 수 있습니다.

시뮬레이터 이미지 :
http://files.droplr.com/files/9979822/aLDJ.Screen%20shot%202011-01-11%20at%2012%3A34%3A34.png http://files.droplr.com/files/9979822/YpCM.Screen%20shot%202011-01-11%20at%2012%3A36%3A47.png

+0

힘든 일을 그것을 실행했을 때 내가 가진 무엇인가? 누군가 도울 수 있습니까? Tumbleweed 배지는 적립 슈퍼 재미가 아니 었어! – eric

+2

샘플 프로젝트를 실행 한 후에 시뮬레이터 나 SDK의 버그라고 생각합니다. 예, iOS 4.3이 설치된 Xcode 4에는 문제가 없습니다. – cxa

+0

고맙습니다. 현재 Xcode/iOS에서 작동해야합니다. – eric

답변

2

에서 잘 작동합니다. 이것은 내가 바로, OS에 망막 시뮬레이터 4.3.2

enter image description here

+0

그것을 보아 주셔서 감사합니다. 나는 이것을 해고하고, 아직도 현재의 OS에서 그렇게하고 있는지 알아보아야 할 것이다. – eric

+1

정확합니다, 그것은 새로운 OS/Xcode에서 수정되었습니다. +1 실제로 내 샘플을 실행하는 대 내게 텍스트를 측정하는 방법을 말하는 ;-) Thanks Sum! – eric

+0

음 .. 나는 iOS8에서 이와 동일한 버그를 보여주었습니다 ... 다시주의를 기울일 시간입니다. 이번에는 기기에서 일어나고 있습니다 !! – eric

1

다음은 내가 동적 텍스트 콘텐츠에 대한 라벨의 높이를 찾기 위해 사용하는 방법입니다. 이것은 내 응용 프로그램 당신의 시뮬레이터에 문제가 될 것으로 보인다

 
 
- (float)getHeightFortheDynamicLabel:(NSString *)stringForTheLabel 
{ 
    UITextView *aSampleTextView; 
    // 30 is the minimum height 
    aSampleTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, mywidth, 30)]; 
    aSampleTextView.text = stringForTheLabel; 
    aSampleTextView.font = [UIFont systemFontOfSize:kMyFontSize]; 
    aSampleTextView.alpha = 0; 
    [self.view addSubview:aSampleTextView]; 
    float textViewHeight = aSampleTextView.contentSize.height; 
    [aSampleTextView removeFromSuperview]; 
    [aSampleTextView release]; 
    return textViewHeight;
}

+0

흥미로운 아이디어 ... 비싼 그대로 ... 나는 신선한 모양을 가져야 할 것입니다 ... 1 년 후 : ...-) – eric

+0

이 방법을 사용하기 전에 방금 시도했습니다 (CGSize theResultSize = [theString sizeWithFont : theFont constrainedToSize : theConstraint]; ) 그다지 정확하지 않았습니다 (왜 그런지 모르겠습니다). 이 방법은 메모리 사용량을 거의 소비하지 않지만, 저에게는 잘 작동합니다. –

0
 NSString *strSubstring = @"asdfghjklasdfghjkl adsds"; 

     CGFloat maxWidth = 205.0; 
     CGFloat maxHeight = 9999; 
     CGSize maximumLabelSize = CGSizeMake(maxWidth,maxHeight); 
     CGSize expectedLabelSize = [strSubstring sizeWithFont:[UIFont fontWithName:@"StagSans-Light" size:12] constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap]; 

     NSLog(@"returned a size h = %f, w = %f", expectedLabelSize.height, expectedLabelSize.width); 

     return expectedLabelSize; 
관련 문제