子类化 UILabel [英] SubClassing UILabel

查看:41
本文介绍了子类化 UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一个站点中阅读了如何插入和 UILabel(子类 UILabel 并覆盖所需的方法).在将它添加到我的应用程序之前,我决定在一个独立的测试应用程序中对其进行测试.代码如下所示.

I read in this same site how to inset and UILabel (subclass UILabel and override the required methods). Before adding it to my app I decided to test it out in a standalone test app. Code is shown below.

这是 MyUILabel.h

#import <UIKit/UIKit.h>

@interface MyUILabel : UILabel

@end

这是 MyUILabel.m

#import "MyUILabel.h"
#import <QuartzCore/QuartzCore.h>

@implementation MyUILabel

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

// for border and rounding
-(void) drawRect:(CGRect)rect
{
    self.layer.cornerRadius = 4.0;
    self.layer.borderWidth = 2;

    [super drawRect:rect];
}

// for inset
-(void) drawTextInRect:(CGRect)rect
{
    UIEdgeInsets insets = {0, 5, 0, 5};

    [super drawTextInRect: UIEdgeInsetsInsetRect(rect, insets)];
}

这是我的 ViewController.h

#import <UIKit/UIKit.h>
#import "MyUILabel.h"


@interface ViewController : UIViewController
{
    MyUILabel   *myDisplay;
}

@property (strong, nonatomic) IBOutlet MyUILabel *myDisplay;

@end

这是 ViewController.m:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize myDisplay;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    myDisplay.text = @"Hello World!";
}

- (void)viewDidUnload
{
    [self setMyDisplay:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

MyUILabel.m 中的所有方法(我覆盖的)都没有被调用.

None of the methods in MyUILabel.m (that Im overriding) get called.

非常感谢对原因的深入了解.

Insights into why are greatly appreciated.

问候,

拉蒙.

推荐答案

好的.我做了一些进一步的挖掘,在 Xcode 中查看 nib 文件时有一个字段可见.它是身份检查员"(左起第三个图标).这需要从 UILabel 更改为 MyUILabel.

Ok. I did some further digging and in Xcode there is a field visible when looking at the nib file. Its the 'Identity Inspector' (3rd icon from left). This needed to be changed from UILabel to MyUILabel.

现在可以了!

这篇关于子类化 UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆