Closed DolphinsLoveCats closed 7 years ago
因业务需求,项目需适配touch4 所以最低版本支持iOS6.0,当在iOS6.0设备上运行时在此段代码layoutAutoHeightWidthView: model: 发生崩溃,后查明原因得知
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);
修改此方法后 UIView+SDAutoLayout.m 内
UIView+SDAutoLayout.m
- (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; } } }
- (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; } } }
希望您采纳
1.sd适配iOS7及以上,iOS6需要用到sd的话可以自行做兼容处理。 2.关于计算文字size,有没有比较好的方法推荐呢?
对于计算文字size,确实没有好的方法,仅仅只能通过减少调用这个来实现优化性能.......
因业务需求,项目需适配touch4 所以最低版本支持iOS6.0,当在iOS6.0设备上运行时在此段代码
layoutAutoHeightWidthView: model:
发生崩溃,后查明原因得知此方法从iOS7.0开始,在iOS6.0时找不到此方法,产生奔溃问题
修改此方法后
UIView+SDAutoLayout.m
内Height的修改
Width的修改
希望您采纳
另外 看到有朋友说在此方法
大量消耗cpu希望可以优化一下,万分感谢.
项目在iOS6.0情况下,demo会有很多错误.....