본문 바로가기

Programming/iOS

[iOS7] sizeWithFont:constrainedToSize:lineBreakMode

반응형

iOS7 으로 버전을 변경하여 빌드를 해보면  sizeWithFont:constrainedToSize:lineBreakMode 로 계산한 값들이 정확하지 않다는 것을 알 수 있다.

구글신의 도움으로 다음과 같은 방법으로 해결하였습니다.

아래 코드는 UILabel의 Category로 구현된 내용입니다.

- (CGSize)boundingSize:(CGSize)size
{
    CGSize resultSize;

#ifdef __IPHONE_7_0
    if ([self.text respondsToSelector:@selector(boundingRectWithSize:options:context:)]) {
        resultSize = [self.text boundingRectWithSize:size
                                             options:NSStringDrawingUsesLineFragmentOrigin
                                          attributes:@{NSFontAttributeName:self.font}
                                             context:nil].size;
    }
    else {
#   pragma clang diagnostic push
#   pragma clang diagnostic ignored "-Wdeprecated-declarations"
        resultSize = [self.text sizeWithFont:self.font
                           constrainedToSize:size
                               lineBreakMode:NSLineBreakByWordWrapping];
#   pragma clang diagnostic pop
    }
#else
#   pragma clang diagnostic push
#   pragma clang diagnostic ignored "-Wdeprecated-declarations"
    resultSize = [self.text sizeWithFont:self.font
                       constrainedToSize:size
                           lineBreakMode:NSLineBreakByWordWrapping];
#   pragma clang diagnostic pop
#endif
    resultSize.width = ceil(resultSize.width);
    resultSize.height = ceil(resultSize.height);
    
    return resultSize;
}


NSString Category 구현


NSString+Size.h

#import 

@interface NSString (Size)

- (CGSize)boundingSize:(CGSize)size font:(UIFont*)font;

@end

NSString+Size.m
#import "NSString+Size.h"

@implementation NSString (Size)

- (CGSize)boundingSize:(CGSize)size font:(UIFont*)font
{
    CGSize resultSize;
    
#ifdef __IPHONE_7_0
    if ([self respondsToSelector:@selector(boundingRectWithSize:options:context:)]) {
        resultSize = [self boundingRectWithSize:size
                                        options:NSStringDrawingUsesLineFragmentOrigin
                                     attributes:@{NSFontAttributeName:font}
                                        context:nil].size;
    }
    else {
#   pragma clang diagnostic push
#   pragma clang diagnostic ignored "-Wdeprecated-declarations"
        resultSize = [self sizeWithFont:font
                      constrainedToSize:size
                          lineBreakMode:NSLineBreakByWordWrapping];
#   pragma clang diagnostic pop
    }
#else
#   pragma clang diagnostic push
#   pragma clang diagnostic ignored "-Wdeprecated-declarations"
    resultSize = [self sizeWithFont:font
                  constrainedToSize:size
                      lineBreakMode:NSLineBreakByWordWrapping];
#   pragma clang diagnostic pop
#endif
    resultSize.width = ceil(resultSize.width);
    resultSize.height = ceil(resultSize.height);
    
    return resultSize;
}

@end


반응형

'Programming > iOS' 카테고리의 다른 글

URL scheme for KakaoOpenSDK is not set properly in plist.  (0) 2014.02.08
Xcode Bots 적용  (0) 2014.02.06
[iOS7] statusBar style 변경  (0) 2013.11.25
푸시 알리 지우기  (1) 2013.11.07
Xcode5 NavigationBar 밑에 View가 있는 문제  (0) 2013.10.14