Get correct text height for iOS 7 in iPhone SDK

瘦欲@ 提交于 2019-12-13 02:28:33

问题


I need to get text height and grow cell's height depending upon text height which increase or decrease dynamically. I now successfully getting correct height of text for iOS 6 and prior but I am not able to get correct height iOS 7 and later. I googled and found many solution but this doesn't worked. You can see attached Image of same cell and see difference in iOS 7 text is truncating and in iOS 6 its showing correct size. I used UILabel. Follow my code of what exactly I am doing. Kindly look into it and guide me better solution if any one experienced this sort of thing earlier.

- (CGSize)getSizeOfText:(NSString *)text withFont:(UIFont *)font widthOftext:(int )txtWidth
{
CGSize boundingSize = CGSizeMake(txtWidth, 9999);
CGSize size;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
    CGRect textRect = [text boundingRectWithSize:boundingSize
                                         options:NSStringDrawingUsesLineFragmentOrigin
                                      attributes:@{NSFontAttributeName:font}
                                         context:nil];
    size=textRect.size;
}
else
{
    CGSize requiredSize = [text sizeWithFont:font constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
    size=requiredSize;
}

return size;
}

On iOS 6 device.

On iOS 7 device.


回答1:


Please try this code

Declare #define kMaxHeight 1000.0f in header.

        CGSize sizeNew = [YourText sizeWithFont:[UIFont fontWithName:Fontname size:intFontSize]
                 constrainedToSize:CGSizeMake(320, kMaxHeight)
                     lineBreakMode:NSLineBreakByWordWrapping];
         txtText.numberOfLines = 0;

If you have any question let m e know.



来源:https://stackoverflow.com/questions/22652743/get-correct-text-height-for-ios-7-in-iphone-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!