如何在xcode中显示UI对象代码?
在我的项目的一部分中,我需要以编程方式创建一些UI对象,我是否可以自定义我的UI对象,如标签,。。。在情节串连板中可视化,然后简单地复制/粘贴与该对象相关的生成代码? 我在xcode菜单中搜索了一下,但没有找到这个,但有一次我在youtube的教程中看到了它如何在xcode中显示UI对象代码?,xcode,Xcode,在我的项目的一部分中,我需要以编程方式创建一些UI对象,我是否可以自定义我的UI对象,如标签,。。。在情节串连板中可视化,然后简单地复制/粘贴与该对象相关的生成代码? 我在xcode菜单中搜索了一下,但没有找到这个,但有一次我在youtube的教程中看到了它 提前感谢是的,您可以自定义UI类或任何其他类,就像我将UILabel类自定义为UILabelExtended一样 UILabelExtended.h 我不是这个意思,让我举个例子:当我添加一个标签并更改其属性时,我如何才能看到xcode为这
提前感谢是的,您可以自定义UI类或任何其他类,就像我将UILabel类自定义为UILabelExtended一样 UILabelExtended.h
我不是这个意思,让我举个例子:当我添加一个标签并更改其属性时,我如何才能看到xcode为这个标签生成的代码,然后简单地将其复制到其他地方使用。要使用此代码,您必须创建UILabelExtended.h和.m文件,从这里复制并粘贴代码。当您必须在该类中使用这个类import UILabelExtended.h时,使用UILabelExtended类的对象,比如:UILabelExtended*lbl=[[UILabelExtended allc]]initWithFrame:yourFrame];或者在IB文件中,获取UILabel并将其类更改为UILabelExtended。现在您可以使用此类中定义的方法,如:[lbl setHeightOfLabel];我希望它能对你有所帮助。谢谢你,伙计,你的答案非常接近我想要的,如果你能给我更多的信息,我将不胜感激,因为我是初学者。我正在使用一个SDK,它允许我在屏幕上添加一个覆盖(即摄像头),因为我想显示更多的对象,我想定义一个UIView,然后添加我想在这个UIView上显示的内容,然后使用以下代码:reader.cameraOverlayView=subView;这很好用。我的问题是创建这些对象并将其添加到子视图中。我尝试过这样做:我添加了TestViewController.m和TestViewController.h,并将其连接到storybord中的视图控制器。然后,我在TestViewController.h中添加了myLabel属性,并根据需要对其进行了自定义,然后我在ViewController.m中导入了TestViewController.h,问题在于,当我想用以下代码将myLabel从TestViewController添加到ViewController.m中的myView时:
code
[self addSubview:testView.myLabel];问题是什么。如果要在ViewController.m中使用myLabel类方法,请在该文件中导入扩展类,并在此处使用扩展方法(如果在typecast nyLabel对象时收到警告)。你明白我的意思了吗?
#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
#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