Amazon.com Widgets UILabel subclassing
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 23, 2013, 11:51:35 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Chapter Study
| | |-+  Chapter 21 - Writing iPhone Applications
| | | |-+  UILabel subclassing
Pages: [1]   Go Down
Print
Author Topic: UILabel subclassing  (Read 829 times)
rrivas
Newbie
*
Posts: 9


Email




« on: April 03, 2012, 08:03:08 PM »

Hi,
   I'm enhancing the fraction calculator. One of the things I would like to do is have my UILabel (results area) somewhat inset and with rounded corners. I did some research and have read that the best way to do this is to subclass UILabel and override the required methods. I did but it doesn't work.

Here's MyUILabel.h

#import <UIKit/UIKit.h>

@interface MyUILabel : UILabel

@end

Here's 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)];
}

Here's my ViewController.h

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


@interface ViewController : UIViewController
{
    MyUILabel   *myDisplay;
}

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

@end

Here's 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

None of the methods in MyUILabel.m get called.

Insights into why are greatly appreciated.

Regards,

Ramon.
Logged
rrivas
Newbie
*
Posts: 9


Email




« Reply #1 on: April 04, 2012, 11:17:53 PM »

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

Now it works! Smiley
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.