文本自适应高度(含适配iOS7)

遇到了需要自适应高度,在iOS6好用的代码在iOS7上被废弃了,下面是在stackoverflow收集到的解决办法。

                if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
                    CGSize size = CGSizeMake(230,9999);
                    CGRect textRect = [specialityObj.name
                                       boundingRectWithSize:size
                                       options:NSStringDrawingUsesLineFragmentOrigin
                                       attributes:@{NSFontAttributeName:[UIFont fontWithName:[AppHandlers zHandler].fontName size:14]}
                                       context:nil];
                    total_height = total_height + textRect.size.height;
                }
                else {
                    CGSize maximumLabelSize = CGSizeMake(230,9999);
                    expectedLabelSize = [specialityObj.name sizeWithFont:[UIFont fontWithName:[AppHandlers zHandler].fontName size:14] constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap]; //iOS 6 and previous.
                    total_height = total_height + expectedLabelSize.height;
                }

 

posted on 2014-02-25 16:56  MarvinDev  阅读(131)  评论(0编辑  收藏  举报

导航