레이블이 boundingRectWithSize:options:attributes:context:인 게시물을 표시합니다. 모든 게시물 표시
레이블이 boundingRectWithSize:options:attributes:context:인 게시물을 표시합니다. 모든 게시물 표시

2014/07/13

[iOS] 크기에 맞게 Font의 크기 줄이기.

Storyboard에서 문자열을 표시를 해야 되는데...
단어나, 길지 않은 문자열은 크기를 200포인트로 표시하고, 만약 길이가 길다면, 그보다 작은 크기로 크기가 맞을 때까지 폰트의 크기를 줄이는 기능이 필요합니다.


//1
+ (UIFont *) spq_getFontForLabel:(NSString *)string font:(UIFont *)font size:(CGSize)size
{
    BOOL isEnough = NO;
    UIFont *afont = font;    //2
    while (!isEnough) {
        //3
        CGRect aboutRect = [string //높이를 구할 NSString
                            boundingRectWithSize:CGSizeMake(size.width*0.9, CGFLOAT_MAX)
                            options:NSStringDrawingUsesLineFragmentOrigin
                            attributes:@{NSFontAttributeName:font}
                            context:nil];
        //4
        if (aboutRect.size.height > (size.height*0.7)) {
            //5
            font = [UIFont fontWithName:font.fontName size:font.pointSize*0.9];
        }else{
            isEnough = YES;
            afont = font;
        }
    }
    return afont;
}

위 소스에서
1: 대상이 되는 문자열과, 그 문자열에 적용되어 있는 Font, 그리고 어떤 너비에서 계산을 해야 되는지 파라미터로 넘겨 줍니다.
2 : while문을 통해서, 크기가 작어질 때까지 적용 합니다.
3 : NSString의 boundingRectWithSize를 이용해서, 대략적인 rect를 구합니다.
     여기에서, 너비의 90%정도를 고려해서 넣습니다. 100% 크기를 하면 옆으로 너무 딱 붙어서 계산이 되어서, 90%크기로 적용해 줍니다.
4 : 계산된 크기가, 대상 높이의 70%보다 크면, 줄여 줍니다. 70%는 대략적으로 생각한 것으로 높의 70%이하가 되는 것이 적당하였습니다. (적용할 때, 변경해서 보시면 됩니다.)
5 : 아직 커서 줄여야 되면, 현재 font 의 name과 크기의 90%를 적용해서 폰트를 다시 만들고 적용해 줍니다.
    이 폰트를 가지고 while문 안에서 다시 계산을 하게 됩니다.

감사합니다.

2014/05/22

[iOS] sizeWithFont를 대체하는 함수 사용법

iOS7이전에는 NSString이 특정 가로 크기에서 줄바꿈 등을 통해서, 높이가 얼마나 되는지 알아보기 위해서는 sizeWithFont:constrainedToSize를 사용하였는데, 7.0에서는 Deprecated가 되어서 변경해야 합니다.
iOS7에서는 font뿐만 아니라, 다른 Attribute들도 높이를 구할 때 필요하므로 font만 참고하는 것이 아니라, attributes라고 NSDictionary를 받아서 구하게 됩니다.

sizeWithFont:constrainedToSize (Deprecated)
    //특정 영역 (268, 4000)의 크기에서 myString에 있는 글의 높이를 iOS6에서 구하는 방법
    UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Light" size:10];
    CGSize aboutSize = [myString sizeWithFont:font constrainedToSize:CGSizeMake(268, 4000)];


Xcode5에서는 boundingRectWithSize:options:attributes:context:를 사용하라고 나옵니다.

    //폰트가 따로 정리가 되어 있지 않는 경우는
    UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Light" size:10];
    CGRect aboutRect = [myString //높이를 구할 NSString 
                          boundingRectWithSize:CGSizeMake(268, CGFLOAT_MAX) 
                                       options:NSStringDrawingUsesLineFragmentOrigin 
                                    attributes:@{NSFontAttributeName:font} 
                                       context:nil];

만약 일반 NSString이 아니고, NSAttributedString을 사용하고 있으면, attributes가 없는 함수를 사용하면 됩니다.

    //폰트가 따로 정리가 되어 있지 않는 경우는
    UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Light" size:10];
    NSAttributedString *myAttributedString = [[NSAttributedString alloc] initWithString:myString
               
    CGRect aboutRect = [myAttributedString //높이를 구할 NSAttributedString
                         boundingRectWithSize:CGSizeMake(268, CGFLOAT_MAX) 
                                      options:NSStringDrawingUsesLineFragmentOrigin
                                      context:nil];                                                              

정리해 둡니다.