iOS

YYText 适配(iOS17)


avatar
博主大大 2023-07-25 2.71k

YYLabel 设置属性(textAlignment)文字居中有可能失效(点击快速直达)

YYText 与 WOCrashProtector 冲突导致 _YYTextKeyboardViewFrameObserver removeFrameObserver 异常闪退(点击快速直达)

iOS17 YYText 使用时会崩溃在 UIGraphicsBeginImageContextWithOptions(点击快速直达)

YYLabel 设置属性(textAlignment)文字居中有可能失效

label.textAlignment = NSTextAlignmentCenter; 
label.attributedText = mutAttStr;

先给文本赋值 后设置文字居中即可

label.attributedText = mutAttStr; 
label.textAlignment = NSTextAlignmentCenter;



YYText 与 WOCrashProtector 冲突导致 _YYTextKeyboardViewFrameObserver removeFrameObserver 异常闪退

__unsafe_unretained 改为 __weak (限iOS11及之后版本使用)

// 原始代码
@implementation _YYTextKeyboardViewFrameObserver {
    __unsafe_unretained UIView *_keyboardView;
}
// 修改后的代码
@implementation _YYTextKeyboardViewFrameObserver {
    __weak UIView *_keyboardView;
}



Q: iOS17 YYText 使用时会崩溃在 UIGraphicsBeginImageContextWithOptions

将 YYTextAsyncLayer 类 _displayAsync 方法里

UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, self.contentsScale);
CGContextRef context = UIGraphicsGetCurrentContext();
if (self.opaque) {
    CGSize size = self.bounds.size;
    size.width *= self.contentsScale;
    size.height *= self.contentsScale;
    CGContextSaveGState(context); {
        if (!self.backgroundColor || CGColorGetAlpha(self.backgroundColor) < 1) {
            CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
            CGContextAddRect(context, CGRectMake(0, 0, size.width, size.height));
            CGContextFillPath(context);
        }
        if (self.backgroundColor) {
            CGContextSetFillColorWithColor(context, self.backgroundColor);
            CGContextAddRect(context, CGRectMake(0, 0, size.width, size.height));
            CGContextFillPath(context);
        }
    } CGContextRestoreGState(context);
}
task.display(context, self.bounds.size, ^{return NO;});
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.contents = (__bridge id)(image.CGImage);

替换为

UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
format.opaque = self.opaque;
format.scale = self.contentsScale;

UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:self.bounds.size format:format];
UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
    CGContextRef context = rendererContext.CGContext;
    if (self.opaque) {
        CGSize size = self.bounds.size;
        size.width *= self.contentsScale;
        size.height *= self.contentsScale;
        CGContextSaveGState(context); {
            if (!self.backgroundColor || CGColorGetAlpha(self.backgroundColor) < 1) {
                CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
                CGContextAddRect(context, CGRectMake(0, 0, size.width, size.height));
                CGContextFillPath(context);
            }
            if (self.backgroundColor) {
                CGContextSetFillColorWithColor(context, self.backgroundColor);
                CGContextAddRect(context, CGRectMake(0, 0, size.width, size.height));
                CGContextFillPath(context);
            }
        } CGContextRestoreGState(context);
    }
    task.display(context, self.bounds.size, ^{return NO;});
}];

self.contents = (__bridge id)(image.CGImage);

相关阅读