1

I have a question about iOS programming.

I am trying to get all subviews from main UIView, and subview properties. I am using delegate "- (void)listSubviewsOfView:(UIView *)view", but this method returns poorly described subview information.

For example:

 - "UIImageView: 0x1d557250; frame = (10 6.5; 32 32); opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = CALayer: 0x1d557220",
 - "UILabel: 0x1d557190; frame = (50 0; 240 43); text = '11:00'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = CALayer: 0x1d557960",

- "UIView: 0x1d5568e0; frame = (110 10; 190 23); layer = CALayer: 0x1d5568b0",

But I want to get, for example, UILabel text color, font size and other information. Is there some kind of method or objects which can help me get this information?

mdoran3844
  • 688
  • 6
  • 14
  • You could create a UIView category and override the `-(NSString *)description;` method. This is what is being displayed. Then you could write it so that it parses all properties. – Fogmeister Aug 28 '13 at 07:38
  • possible answer http://zearfoss.wordpress.com/2011/04/14/objective-c-quickie-printing-all-declared-properties-of-an-object/ – Injectios Aug 28 '13 at 07:38

2 Answers2

1
#import <objc/runtime.h>

- (void)yourMethod{
    UIView *parnetView = ...;

    [parentView.subviews makeObjectsPerformSelector:@selector(printAllProperties)];
}

@interface UIView (printAllProperties)
- (void) printAllProperties;
@end

@implementation UIView (printAllProperties)

-(void)printAllProperties{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        unsigned int numberOfProperties = 0;
        objc_property_t *propertyArray = class_copyPropertyList([self class], &numberOfProperties);
        for (NSUInteger i = 0; i < numberOfProperties; i++) {
            objc_property_t property = propertyArray[i];
            NSString *name = [[NSString alloc] initWithUTF8String:property_getName(property)];
            NSLog(@"Property %@ Value: %@", name, [self valueForKey:name]);
        }
        free(propertyArray);
    });    
}
@end

You need to add method printAllProperties as category to UIView

Rubycon
  • 18,156
  • 10
  • 49
  • 70
  • Thanks. With a little bit of editing ... and all worked out great. Got what I wanted. But I have a question, can I get this working in GCD, or just on another thread? – DeveloperMaris Aug 30 '13 at 07:54
0

AFAIK there no method available in UIKit which will do that for you. You will have to query the runtime system to get this information. Refer to this answer for more details (Upvote the answer in the link if you find it useful).

For understanding about ObjC runtime, read through Objective-C Runtime Programming Guide and Objective-C Runtime Reference.

Hope that helps!

Community
  • 1
  • 1
Amar
  • 13,202
  • 7
  • 53
  • 71