Sishu
Sishu

Reputation: 1558

Customize UILabel's Behavior

I am new to iphone.i found UILabel instance methodes very hard for me to implement.how can i use it.how can i customize the appearance of your text of a UIlabel further by subclassing UILabel.Plz i need little help to initiate.Foe example i have a label in my viewController how can i custmize it's text and hoe to subclass .thanks in advance.

Upvotes: 0

Views: 287

Answers (1)

Kanan Vora
Kanan Vora

Reputation: 2122

You can use so many properties of a UILabel like:

UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 100, 40)];
lbl.font = [UIFont fontWithName:@"Helvetica" size:12.0];    // For setting font style with size
lbl.textColor = [UIColor whiteColor];        //For setting text color
lbl.backgroundColor = [UIColor clearColor];  // For setting background color
lbl.textAlignment = UITextAlignmentCenter;   // For setting the horizontal text alignment
lbl.numberOfLines = 2;                       // For setting allowed number of lines in a label
lbl.lineBreakMode = UILineBreakModeWordWrap; // For setting line break mode
lbl.text = @"TitleText";                            // For setting the text inside the label

Let me know if any thing other than this you want to know!!

The two methods

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
    return CGRectInset(bounds, MARGIN, MARGIN);
}

- (void)drawTextInRect:(CGRect)rect
{
    [super drawTextInRect: CGRectInset(self.bounds, MARGIN, MARGIN)];
}

We are using CGRectInset to create a rectangle that is either larger or smaller than an existing rectangle (bounds).

For Smaller rectangle, use positive values as MARGIN For Larger rectangle, use positive values as MARGIN

All the best!!!

Upvotes: 3

Related Questions