开发随笔2023Q1
2022-11-28 本文已影响0人
大成小栈
- 将UIView转换为图片
//初始化并绘制UI
HeadImageView *view = [[HeadImageView alloc] initWithFrame:frame BackGroundColor:backGroundColor Text:text TextColor:textColor TextFontOfSize:size];
//转化成image
UIGraphicsBeginImageContext(view.bounds.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[view.layer renderInContext:ctx];
UIImage* tImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tImage;
- 计算UILabel内容的实际高度、行数
CGSize textMaxSize = CGSizeMake(UILabel的指定宽度, MAXFLOAT);
NSDictionary *textAttrs = @{NSFontAttributeName : 字体大小};
CGFloat textH(UILabel内容的实际高度) = [UILabel的属性名 boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:textAttrs context:nil].size.height;
NSNumber *count(UILabel内容的行高) = @((textH) / _label.font.lineHeight);
NSLog(@"共 %td 行", [count integerValue]);
- 成员变量和属性
以下两种定义变量的方式,是有明显区别的。具体体现在self. brandImageView 和 strongSelf->_brandImageView调用的时候,尤其是在异步block回调中,..->_brandImageView会产生崩溃。
/// 1.
@interface ActivitySlidStoreItemCell()
@property (nonatomic, strong) UIImageView *brandImageView;
@property (nonatomic, strong) UIImageView *bgImageView;
@end
/// 2.
@interface ActivitySlidStoreItemCell() {
UIImageView *_bgImageView;
UIImageView *_brandImageView;
}
@end