关于UIPreviewAction字体颜色的黑魔法

**** 在用到3D-Touch的时候,想到了UIPreviewAction随性就写了一下,但是看到字体的颜色不能修改,这时候就想到了用一个运行时尝试修改一下,没想到还真成功了.下面上代码 ****

@property (nonatomic,strong) UIColor *tintColor; /**< 统一按钮样式 不写系统默认的蓝色*/

- (UIColor *)tintColor {

return objc_getAssociatedObject(self, @selector(tintColor));
}
- (void)setTintColor:(UIColor *)tintColor {

unsigned int count = 0;
Ivar *ivars = class_copyIvarList([UIPreviewAction class], &count);
for(int i = 0;i < count;i ++){
    
    Ivar ivar = ivars[i];
    NSString *ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding];
    NSLog(@"%@",ivarName);
    //标题颜色
    if ([ivarName isEqualToString:@"_color"] && tintColor) {
        
        [self setValue:tintColor forKey:@"color"];
    }
}

free(ivars);


objc_setAssociatedObject(self, @selector(tintColor), tintColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

这时候在外部调用

#pragma mark -- /*3DTouch-Delegate*/ 
- (NSArray> *)previewActionItems {

    __weak typeof(self) weakSelf = self;

    UIPreviewAction *saveAction = [UIPreviewAction actionWithTitle:@"保存到相册" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
    
        UIImageWriteToSavedPhotosAlbum(weakSelf.imageView.image, weakSelf, @selector(image:didFinishSavingWithError:contextInfo:),nil);
    
}];
    saveAction.tintColor = [UIColor blackColor];
    
    UIPreviewAction *cancellAction = [UIPreviewAction actionWithTitle:@"取消" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
    
}];
    cancellAction.tintColor = [UIColor blackColor];
    return @[saveAction,cancellAction];
}

你可能感兴趣的:(关于UIPreviewAction字体颜色的黑魔法)