欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

输出UIView的详细信息

程序员文章站 2022-07-15 16:42:11
...

主要是给UIView写了一个category,输出主要包括了UIView的frame、类名、子视图等。

 

UIView+FullDescription.h

 

#import <UIKit/UIKit.h>

@interface UIView (FullDescription)

- (NSMutableDictionary *)fullDescription;

@end
  

UIView+FullDescription.m

 

#import "UIView+FullDescription.h"

@implementation UIView (FullDescription)

/**
* Builds a tree of data about all the views starting at this view
* and traversing all subviews. Data includes:
*	- className (name of the subclass of UIView)
*	- address (address in memory)
*	- tag
*	- text (if any)
*	- title (if any)
*	- subviews (recursive structures)
**/
- (NSMutableDictionary *)fullDescription {
	NSDictionary *frame =
    [NSDictionary dictionaryWithObjectsAndKeys:
     [NSNumber numberWithFloat:self.frame.origin.x], @"x",
     [NSNumber numberWithFloat:self.frame.origin.y], @"y",
     [NSNumber numberWithFloat:self.frame.size.width], @"width",
     [NSNumber numberWithFloat:self.frame.size.height], @"height",
     nil];
	NSMutableDictionary *description =
    [NSMutableDictionary dictionaryWithObjectsAndKeys:
     [NSNumber numberWithInteger:(NSInteger)self], @"address",
     NSStringFromClass([self class]), @"className",
     frame, @"frame",
     [NSNumber numberWithInteger:[self tag]], @"tag",
     [self valueForKeyPath:@"subviews.fullDescription"], @"subviews",
     nil];
	
	if ([self respondsToSelector:@selector(text)])
		[description setValue:[self performSelector:@selector(text)] forKey:@"text"];
    
	if ([self respondsToSelector:@selector(title)])
		[description setValue:[self performSelector:@selector(title)] forKey:@"title"];
	
	if ([self respondsToSelector:@selector(currentTitle)])
		[description setValue:[self performSelector:@selector(currentTitle)] forKey:@"currentTitle"];
	
	return description;
}

@end
相关标签: iPhone iOS UIView