iOS調(diào)試小助手QuickLook

我們在調(diào)試iOS的時候常常會用到下面這個小功能。



Debug時鼠標移動到變量上,等到輔助工具欄出現(xiàn),點擊眼睛按鈕就可以預覽這個變量的內(nèi)容。最方便的地方莫過于可以預覽圖片,甚至是View。蘋果內(nèi)置了幾種可以快速預覽的類型。


上面有些是iOS的類型有些是Mac App中的類型,具體的效果大家可以自行嘗試。本文將要介紹的是如何給自己的類添加快速預覽效果。

實現(xiàn)效果

QuickLookTarget是實現(xiàn)了自定義快速預覽的類。在Debug時快速預覽效果如下。


圖中QuickLookTarget的三個屬性都在快速預覽中顯示了出來。

QuickLookTarget結(jié)構(gòu)

QuickLookTarget類有三個屬性。兩個字符串和一個圖片。

@property (copy, nonatomic) NSString *title;
@property (copy, nonatomic) NSString *content;
@property (strong, nonatomic) UIImage *headImage;

支持快速預覽需要做的事

QuickLookTarget類中增加- (id)debugQuickLookObject方法即可支持快速預覽,debugQuickLookObject方法可以返回任何蘋果支持的內(nèi)置快速預覽類型。本文使用的是圖片UIImage。通過UIKit將屬性信息繪制到圖片上,然后返回。

- (id)debugQuickLookObject {
    CGSize imageSize = CGSizeMake(400, 700);
    UIGraphicsBeginImageContext(imageSize);
    uint32_t propertyCount;
    objc_property_t * propertyList = class_copyPropertyList(self.class, &propertyCount);
    CGFloat currentYLoc = 10;
    for (int i = 0; i < propertyCount; ++i) {
        objc_property_t property = *propertyList;
        NSString *propertyName = [NSString stringWithUTF8String:property_getName(property)];
        NSString *propertyAttrs = [NSString stringWithUTF8String:property_getAttributes(property)];
        if ([propertyAttrs containsString: @"NSString"]) {
            NSString *propertyValue = [self valueForKey:propertyName];
            NSString *displayText = [NSString stringWithFormat:@"%@:\n %@", propertyName, propertyValue];
            CGRect textRect = [displayText boundingRectWithSize:CGSizeMake(imageSize.width, imageSize.height) options:NSStringDrawingUsesLineFragmentOrigin  attributes:nil context:nil];
            [displayText drawWithRect:CGRectMake(textRect.origin.x, currentYLoc + textRect.origin.y, textRect.size.width, textRect.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil];
            currentYLoc += textRect.size.height + 10;
        } else if ([propertyAttrs containsString: @"UIImage"]) {
            NSString *displayText = [NSString stringWithFormat:@"%@: ", propertyName];
            CGRect textRect = [displayText boundingRectWithSize:CGSizeMake(imageSize.width, imageSize.height) options:NSStringDrawingUsesLineFragmentOrigin  attributes:nil context:nil];
            [displayText drawWithRect:CGRectMake(textRect.origin.x, currentYLoc + textRect.origin.y, textRect.size.width, textRect.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil];
            currentYLoc += textRect.size.height + 10;
            
            UIImage *propertyValue = [self valueForKey:propertyName];
            [propertyValue drawInRect:CGRectMake(0, currentYLoc, imageSize.width, imageSize.width / propertyValue.size.width * propertyValue.size.height)];
            currentYLoc += imageSize.width / propertyValue.size.width * propertyValue.size.height;
        }
        propertyList++;
    }
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    return image;
}

debugQuickLookObject中使用runtime獲取所有屬性,然后遍歷屬性繪制字符串和圖片類型的數(shù)據(jù)。對于快速預覽的支持就到此結(jié)束了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容