[objective-c] sizeWithFont 메소드는 더 이상 사용되지 않습니다. boundingRectWithSize가 예기치 않은 값을 반환합니다.

iOS7에서는 sizeWithFont더 이상 사용되지 않으므로 boundingRectWithSize(CGRect 값을 반환하는) 사용하고 있습니다. 내 코드 :

 UIFont *fontText = [UIFont fontWithName:[AppHandlers zHandler].fontName size:16];
                    // you can use your font.

 CGSize maximumLabelSize = CGSizeMake(310, 9999);

 CGRect textRect = [myString boundingRectWithSize:maximumLabelSize
                             options:NSStringDrawingUsesLineFragmentOrigin
                             attributes:@{NSFontAttributeName:fontText}
                             context:nil];

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

에서를 사용할 때와 다른 크기 인 textRect내 . 이 문제를 어떻게 해결할 수 있습니까?maximumLabelSizesizeWithFont



답변

새 라벨을 만들고 사용하는 것은 어떻습니까 sizeThatFit:(CGSize)size??

UILabel *gettingSizeLabel = [[UILabel alloc] init];
gettingSizeLabel.font = [UIFont fontWithName:@"YOUR FONT's NAME" size:16];
gettingSizeLabel.text = @"YOUR LABEL's TEXT";
gettingSizeLabel.numberOfLines = 0;
gettingSizeLabel.lineBreakMode = NSLineBreakByWordWrapping;
CGSize maximumLabelSize = CGSizeMake(310, CGFLOAT_MAX);

CGSize expectSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];

편집 :이 상단 코드는 iOS 7 이상에 적합하지 않으므로 아래를 사용하십시오.

CGRect textRect = [myString boundingRectWithSize:maximumLabelSize
                         options:NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingUsesFontLeading
                         attributes:@{NSFontAttributeName:fontText}
                         context:nil];


답변

답변 에서 제안하는 방법에 추가 옵션을 제공해야 할 수도 있습니다 .

CGSize maximumLabelSize = CGSizeMake(310, CGFLOAT_MAX);
CGRect textRect = [myString boundingRectWithSize:maximumLabelSize
                                         options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                      attributes:@{NSFontAttributeName: fontText}
                                         context:nil];


답변

내 작업 코드 스 니펫은 다음과 같습니다.

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:attributeDict];

NSString *headline  = [dict objectForKey:@"title"];
UIFont *font        = [UIFont boldSystemFontOfSize:18];
CGRect  rect        = [headline boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil];

CGFloat height      = roundf(rect.size.height +4)

계산 된 높이에 4px를 추가했습니다.이 4px가 없으면 한 줄이 누락 되었기 때문입니다.

이 코드 조각을 tableView에서 사용하고 NSNumbers 배열에 “높이”를 추가하면 기본 textLabel에 대한 올바른 셀 높이를 얻습니다.

textLabel의 텍스트 아래에 더 많은 공간을 원하면 4 픽셀을 더 추가합니다.

**** 업데이트 ****

저는 “40px의 너비 버그”에 동의하지 않습니다. 4px가 글자와 한 줄의 경계 사이의 기본 높이이기 때문에 누락 된 높이의 4px라고 외칩니다. UILabel로 확인할 수 있으며, 글꼴 크기가 16 인 경우 UILabel 높이가 20이 필요합니다.

그러나 마지막 줄에 “g”가 없거나 그 안에 아무것도 없으면 측정 값이 4px의 높이를 놓칠 수 있습니다.

나는 약간의 방법으로 그것을 다시 확인했고, 내 라벨의 정확한 높이는 20,40 또는 60이고 오른쪽 너비는 300px 미만입니다.

iOS6 및 iOS7을 지원하려면 내 방법을 사용할 수 있습니다.

- (CGFloat)heightFromString:(NSString*)text withFont:(UIFont*)font constraintToWidth:(CGFloat)width
{
    CGRect rect;

    float iosVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (iosVersion >= 7.0) {
        rect = [text boundingRectWithSize:CGSizeMake(width, 1000) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil];
    }
    else {
        CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(width, 1000) lineBreakMode:NSLineBreakByWordWrapping];
        rect = CGRectMake(0, 0, size.width, size.height);
    }
    NSLog(@"%@: W: %.f, H: %.f", self, rect.size.width, rect.size.height);
    return rect.size.height;
}

**** 업그레이드 ****

귀하의 의견 덕분에 다음과 같이 기능을 업그레이드했습니다. sizeWithFont는 더 이상 사용되지 않으며 XCode에서 경고를 받게되므로이 특정 함수 호출 / 코드 블록에 대한 경고를 제거하기 위해 diagnostic-pragma-code를 추가했습니다.

- (CGFloat)heightFromStringWithFont:(UIFont*)font constraintToWidth:(CGFloat)width
{
    CGRect rect;

    if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
        rect = [self boundingRectWithSize:CGSizeMake(width, 1000) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil];
    }
    else {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
        CGSize size = [self sizeWithFont:font constrainedToSize:CGSizeMake(width, 1000) lineBreakMode:NSLineBreakByWordWrapping];
        rect = CGRectMake(0, 0, size.width, size.height);
#pragma GCC diagnostic pop
    }
    return ceil(rect.size.height);
}

4px 주제 : 사용하는 글꼴 및 글꼴 두께에 따라 계산에서 다른 높이 값을 반환합니다. 필자의 경우 글꼴 크기가 16.0 인 HelveticaNeue-Medium은 한 줄의 경우 20.0의 줄 높이를 반환하지만 두 줄의 경우 39.0, 4 줄의 경우 78px-> 줄 2로 시작하는 모든 줄에 대해 1px 누락-하지만 원하는 경우 모든 라인에 대해 fontsize + 4px 라인 스페이스를 가지려면 높이 결과를 얻어야합니다.

코딩하는 동안 명심하십시오!

아직이 “문제”에 대한 기능이 없지만 완료되면이 게시물을 업데이트하겠습니다.


답변

내가 올바르게 이해한다면, 당신은 sizeWithFont로 얻을 수있는 크기를 얻는 방법으로 boundingRectWithSize를 사용하고 있습니까 (CGRect가 아닌 CGSize를 직접 원한다는 의미)?

이것은 당신이 찾고있는 것 같습니다.

더 이상 사용되지 않는 sizeWithFont 대체 : iOS 7에서?

sizeWithFont 대신 sizeWithAttributes :를 사용하여 크기를 가져옵니다.

다음과 같은 것을 사용하여 여전히 잘못된 크기를 얻습니까?

UIFont *fontText = [UIFont fontWithName:[AppHandlers zHandler].fontName size:16];
                    // you can use your font.

expectedLabelSize = [myString sizeWithAttributes:@{NSFontAttributeName:fontText}];


답변

@SoftDesigner의 의견이 저에게 효과적이었습니다.

CGRect descriptionRect = [description boundingRectWithSize:CGSizeMake(width, 0)
                                                       options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                                    attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12]}
                                                       context:nil];
result = ceil(descriptionRect.size.height);


답변

-boundingRectWithSize : options : attributes : context : method 를 사용하는 대신 iOS 7.0 에서 레이블 런타임 sizewithfont의 크기를 찾는 데 더 이상 사용되지 않습니다 .

아래 코드와 같이 사용할 수 있습니다.

CGSize constraint = CGSizeMake(MAXIMUM_WIDHT, TEMP_HEIGHT);
NSRange range = NSMakeRange(0, [[self.message body] length]);

NSDictionary *attributes = [YOUR_LABEL.attributedText attributesAtIndex:0 effectiveRange:&range];
CGSize boundingBox = [myString boundingRectWithSize:constraint options:NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
int numberOfLine = ceil((boundingBox.width) / YOUR_LABEL.frame.size.width);
CGSize descSize = CGSizeMake(ceil(boundingBox.width), ceil(self.lblMessageDetail.frame.size.height*numberOfLine));

CGRect frame=YOUR_LABEL.frame;
frame.size.height=descSize.height;
YOUR_LABEL.frame=frame;

여기에서 높이 또는 너비를 찾기 위해 너비를 최대 길이로 제공해야합니다.

나를 위해 일하는 이것을 시도하십시오.


답변