如何在xcode中显示UI对象代码?

时间:2013-06-11 20:01:09

标签: xcode

在我的项目的一部分中,我需要以编程方式创建一些UI对象,我是否可以自定义我的UI对象,如标签,...在storyboard中可视化,然后只需复制/粘贴与该对象相关的生成代码? 我在xcode菜单中搜索但我找不到这个,但是一旦我在youtube的教程中看到它。

先谢谢

1 个答案:

答案 0 :(得分:0)

是的,您可以自定义UI类或任何其他类,就像我已将UILabel类自定义为UILabelExtended

  

UILabelExtended.h

#import <Foundation/Foundation.h>

/*  **********************************************************************************************
        This class inherit the class UILabel and extend the features of UILabel. 
    ********************************************************************************************** */
@interface UILabelExtended : UILabel {
   __unsafe_unretained id  customDelegate;
    id  objectInfo;
    SEL selector;
}

@property (nonatomic,assign) SEL selector;;
@property (nonatomic,assign) id  customDelegate;
@property (nonatomic,retain) id  objectInfo;
@end

@interface UILabel(UILabelCategory)
- (void)setHeightOfLabel;
- (void)setWidthOfLabel;
- (void)setHeightOfLabelWithMaxHeight:(float)maxHeight;
- (void)setWidthOfLabelWithMaxWidth:(float)maxWidth ;
@end
  

UILabelExtended.m

#import "UILabelExtended.h"


@implementation UILabelExtended
@synthesize selector,customDelegate, objectInfo;

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if(self.selector)
        if([self.customDelegate respondsToSelector:self.selector]) {
            [self.customDelegate performSelector:self.selector withObject:self];
            return;
        }
}

- (void)dealloc {

    self.customDelegate = nil;
    self.selector = NULL;
    self.objectInfo = nil;
}
@end


@implementation UILabel(UILabelCategory)

- (void)setHeightOfLabel {
    UILabel* label = self;

    //get the height of label content
    CGFloat height = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(label.bounds.size.width, 99999) lineBreakMode:NSLineBreakByWordWrapping].height;
    //set the frame according to calculated height
    CGRect frame = label.frame;
    if([label.text length] > 0) {

        frame.size.height = height;
    } 
    else {
        frame.size.height = 0;
    }
    label.frame = frame;
}


- (void)setWidthOfLabel {
    UILabel* label = self;

        //get the height of label content
    CGFloat width = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(99999, label.bounds.size.height) lineBreakMode:NSLineBreakByWordWrapping].width;
        //set the frame according to calculated height
    CGRect frame = label.frame;
    if([label.text length] > 0) {

        frame.size.width = width+5;
    } 
    else {
        frame.size.width = 0;
    }
    label.frame = frame;
}

- (void)setHeightOfLabelWithMaxHeight:(float)maxHeight {
    UILabel* label = self;

    //get the height of label content
    CGFloat height = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(label.bounds.size.width, maxHeight) lineBreakMode:NSLineBreakByWordWrapping].height;
    //set the frame according to calculated height
    CGRect frame = label.frame;

    if([label.text length] > 0) {
        if (height > maxHeight) {
            frame.size.height = maxHeight;
        }
        else {
            frame.size.height = height;
        }

    } 
    else {
        frame.size.height = 0;
    }
    label.frame = frame;
}

- (void)setWidthOfLabelWithMaxWidth:(float)maxWidth  {
    UILabel* label = self;

    //get the height of label content
    CGFloat width = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(99999, label.bounds.size.height) lineBreakMode:NSLineBreakByWordWrapping].width;
    //set the frame according to calculated height
    CGRect frame = label.frame;
    if([label.text length] > 0) {

        if (width > maxWidth) {
            frame.size.width = maxWidth;
        }
        else {
            frame.size.width = width;
        }
    } 
    else {
        frame.size.width = 0;
    }
    label.frame = frame;
}
@end