遍历subviews

前端之家收集整理的这篇文章主要介绍了遍历subviews前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. NSString *xml = [self digView:self.view];
  2. [xml writeToFile:@"/Users/frankhou/Desktop/view.xml"
  3. atomically:YES
  4. encoding:NSUTF8StringEncoding
  5. error:nil];
  1. - (NSString *)digView:(UIView *)view
  2. {
  3. if ([view isKindOfClass:[UITableViewCell class]]) return @"";
  4. // 1.初始化
  5. NSMutableString *xml = [NSMutableString string];
  6.  
  7. //2.标签开头
  8. [xml appendFormat:@"<%@ frame=\"%@\"",view.class,NSStringFromCGRect(view.frame)];
  9. if (!CGPointEqualToPoint(view.bounds.origin,CGPointZero)) {
  10. [xml appendFormat:@" bounds=\"%@\"",NSStringFromCGRect(view.bounds)];
  11. }
  12. if ([view isKindOfClass:[UIScrollView class]]) {
  13. UIScrollView *scroll = (UIScrollView *)view;
  14. if (!UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero,scroll.contentInset)) {
  15. [xml appendFormat:@" contentInset=\"%@\"",NSStringFromUIEdgeInsets(scroll.contentInset)];
  16. }
  17. }
  18.  
  19. //3.判断是否要结束
  20. if (view.subviews.count == 0) {
  21. [xml appendString:@" />"];
  22. return xml;
  23. } else {
  24. [xml appendString:@">"];
  25. }
  26.  
  27. //4.遍历所有子控件
  28. for (UIView*child in view.subviews) {
  29. NSString *childXml = [self digView:child];
  30. [xml appendString:childXml];
  31. }
  32.  
  33. //5.标签结尾
  34. [xml appendFormat:@"</%@>",view.class];
  35.  
  36. return xml;
  37. }

猜你在找的XML相关文章