5

以下のコードを使用して、文字列の長さからラベルの高さを計算しています。xcode 5.0 を使用しており、iOS 6 シミュレーターでは正常に動作しますが、iOS 7 ではうまく動作しません。

NSString* str = [[array objectAtIndex:i]valueForKey:@"comment"];

CGSize size = [className sizeWithFont:[UIFont systemFontOfSize:15]   
 constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

Height_1 = size.height;

iOS 7 の解決策があれば、助けてください。前もって感謝します

4

4 に答える 4

21

iOS 6 と iOS 7 の高さを計算するために使用するソリューションを次に示します。再利用可能にするためにいくつかの引数を渡しました。

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

/**
*  This method is used to calculate height of text given which fits in specific width having    font provided
*
*  @param text       Text to calculate height of
*  @param widthValue Width of container
*  @param font       Font size of text
*
*  @return Height required to fit given text in container
*/

+ (CGFloat)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font
{
    CGFloat result = font.pointSize + 4;
    if (text)
    {
        CGSize textSize = { widthValue, CGFLOAT_MAX };       //Width and height of text area
        CGSize size;
        if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
        {
            //iOS 7
            CGRect frame = [text boundingRectWithSize:textSize
                                          options:NSStringDrawingUsesLineFragmentOrigin
                                       attributes:@{ NSFontAttributeName:font }
                                          context:nil];
            size = CGSizeMake(frame.size.width, frame.size.height+1);
        }
        else
        {
            //iOS 6.0
            size = [text sizeWithFont:font constrainedToSize:textSize lineBreakMode:NSLineBreakByWordWrapping];
        }
        result = MAX(size.height, result); //At least one row
    }
    return result;
}

これが役に立てば幸いです。はい、どんな提案でも大歓迎です。ハッピーコーディング:)

iOS 7 以降の場合、以下の方法を使用します。

+ (CGSize)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
    CGSize size = CGSizeZero;
    if (text) {
        //iOS 7
        CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:font } context:nil];
        size = CGSizeMake(frame.size.width, frame.size.height + 1);
    }
    return size;
}
于 2014-02-25T12:31:16.897 に答える
2

これを使ってみてください

#define FONT_SIZE 15.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 20.0f


NSString *text;
CGSize constraint;
CGSize size;
CGFloat height;

text = [[array objectAtIndex:i]valueForKey:@"comment"];
constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

CGRect textRect = [text boundingRectWithSize:constraint
                                    options:NSStringDrawingUsesLineFragmentOrigin
                                    attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:FONT_SIZE]}
                                    context:nil];

size = textRect.size;


height = size.height;
于 2014-02-25T12:31:08.597 に答える
1
sizeWithFont

廃止されました。使用する

[string sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];

代わりは

于 2014-02-25T12:28:07.600 に答える
-1

以下のコードを使用して、ラベルの高さを取得します

CGSize szMaxCell = CGSizeMake(220, 2999);
UIFont *font = [UIFont systemFontOfSize:14.0f];    // whatever font you're using to display
CGSize szCell = [yourString sizeWithFont:font constrainedToSize:szMaxCell lineBreakMode:NSLineBreakByWordWrapping];
于 2014-02-25T12:29:09.327 に答える