gsdios / SDAutoLayout

One line of code to implement automatic layout. 一行代码搞定自动布局!支持Cell和Tableview高度自适应,Label和ScrollView内容自适应,致力于做最简单易用的AutoLayout库。The most easy way for autoLayout. Based on runtime.
MIT License
5.9k stars 1.28k forks source link

layoutAutoHeightWidthView: model: 方法崩溃问题 #179

Closed DolphinsLoveCats closed 7 years ago

DolphinsLoveCats commented 7 years ago

因业务需求,项目需适配touch4 所以最低版本支持iOS6.0,当在iOS6.0设备上运行时在此段代码layoutAutoHeightWidthView: model: 发生崩溃,后查明原因得知

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary<NSString *, id> *)attributes context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11, 7_0);

此方法从iOS7.0开始,在iOS6.0时找不到此方法,产生奔溃问题

修改此方法后 UIView+SDAutoLayout.m

Height的修改

- (void)layoutAutoHeightWidthView:(UIView *)view model:(SDAutoLayoutModel *)model
{
    if ([view.autoHeightRatioValue floatValue] > 0) {
        view.height_sd = view.width_sd * [view.autoHeightRatioValue floatValue];
    } else {
        if ([view isKindOfClass:[UILabel class]]) {
            UILabel *label = (UILabel *)view;
            label.numberOfLines = 0;
            if (label.text.length) {
                if (!label.isAttributedContent) {
                    //*********************修改块******start*************************
                    CGSize labelSize;

                    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) {
                        labelSize = [label.text sizeWithFont:label.font
                                           constrainedToSize:CGSizeMake(label.width_sd, MAXFLOAT)
                                               lineBreakMode:NSLineBreakByWordWrapping];
                    }else{

                        CGRect rect = [label.text boundingRectWithSize:CGSizeMake(label.width_sd, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : label.font} context:nil];
                        labelSize =rect.size;
                    }
                    label.height_sd = labelSize.height + 0.1;
                    //*********************修改块*******end*************************
                } else {
                    [label sizeToFit];
                }
            } else {
                label.height_sd = 0;
            }
        } else {
            view.height_sd = 0;
        }
    }
}

Width的修改

- (void)layoutAutoWidthWidthView:(UIView *)view model:(SDAutoLayoutModel *)model
{
    if ([view isKindOfClass:[UILabel class]]) {
        UILabel *label = (UILabel *)view;
        CGFloat width = [view.sd_maxWidth floatValue] > 0 ? [view.sd_maxWidth floatValue] : MAXFLOAT;
        label.numberOfLines = 1;
//*********************修改块******start*************************
        if (label.text.length) {
            if (!label.isAttributedContent) {
                CGSize labelSize;

                if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) {
                    labelSize = [label.text sizeWithFont:label.font
                                       constrainedToSize:CGSizeMake(MAXFLOAT, label.height_sd)
                                           lineBreakMode:NSLineBreakByWordWrapping];
                }else{

                    CGRect rect = [label.text boundingRectWithSize:CGSizeMake(MAXFLOAT, label.height_sd) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : label.font} context:nil];
                    labelSize =rect.size;
                }
                if (labelSize.width > width) {
                    labelSize.width = width;
                }
                label.width_sd = labelSize.width + 0.1;
//*********************修改块*******end*************************

            } else{
                [label sizeToFit];
                if (label.width_sd > width) {
                    label.width_sd = width;
                }
            }
        } else {
            label.size_sd = CGSizeZero;
        }
    }
}

希望您采纳

另外 看到有朋友说在此方法

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary<NSString *, id> *)attributes context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11, 7_0);

大量消耗cpu希望可以优化一下,万分感谢.

项目在iOS6.0情况下,demo会有很多错误.....

gsdios commented 7 years ago

1.sd适配iOS7及以上,iOS6需要用到sd的话可以自行做兼容处理。 2.关于计算文字size,有没有比较好的方法推荐呢?

DolphinsLoveCats commented 7 years ago

对于计算文字size,确实没有好的方法,仅仅只能通过减少调用这个来实现优化性能.......