Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reflect properties of UILabel to another?

Im trying to customize UITableViewController dynamically. So i have changed many properties of cell.textLabel. Now i want to copy these properties to detailTextLabel and to one label i have created through code. How it can be done?

    cell.textLabel.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
    cell.textLabel.textColor=[UIColor whiteColor];
    cell.textLabel.font=[UIFont fontWithName:@"HelveticaNeue" size:26];
    cell.textLabel.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;

This is my cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.textLabel.text=[_names objectAtIndex:indexPath.row];
        cell.textLabel.tag=indexPath.row;

        cell.detailTextLabel.text=[_phones objectAtIndex:indexPath.row];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png"] ];
        [imageView setFrame:CGRectMake(380,10,30,50)];
        [cell addSubview:imageView];
        //customize the seperator
        UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1000, 1)];/// change size as you need.
        separatorLineView.backgroundColor = [UIColor grayColor];// you can also put image here
        [cell.contentView addSubview:separatorLineView];
        cell.contentView.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
        cell.textLabel.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
        cell.textLabel.textColor=[UIColor whiteColor];
        cell.textLabel.font=[UIFont fontWithName:@"HelveticaNeue" size:26];
        cell.textLabel.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
        //here i want to copy the properties
        return cell;
    }
like image 316
Saranjith Avatar asked Sep 28 '16 09:09

Saranjith


3 Answers

For Swift3

class MyLabel: UILabel {
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        backgroundColor = UIColor(red: 0, green: 0.188235, blue: 0.313725, alpha: 1)
        textColor = UIColor.white
        font = UIFont(name: "HelveticaNeue", size: 26)
        autoresizingMask = .flexibleRightMargin
    }
}

Create a subclass of UILabel in this way.

#import "MyLabel.h"

@implementation MyLabel

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    self.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
    self.textColor=[UIColor whiteColor];
    self.font=[UIFont fontWithName:@"HelveticaNeue" size:26];
    self.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
}

@end

And now create an object of this MyLabel and your properties will be set automatically and also just assign this class to your label through storyboard to the label in your cell.

Subclassing is the best way for implementing reusable code.

Or else you can even create an extension of class or even an class method in some class which accepts the UILabel and sets the properties but this all are not the best practices. Another problem with extensions is the you can only use self but not super. This may create problems in future when you have to extend the properties.

I hope I am clear and helpful.

like image 191
Nikhil Manapure Avatar answered Oct 20 '22 01:10

Nikhil Manapure


You can use this method to make all the labels of UITabelViewCell to same property

Here just loop through the subViews and check whether the subview is of UILabel, If it is of UILabel then set the property you want.

My Code :

- (void)formatTheLabelForCell:(UITableViewCell *)cell
{
    for (UIView *view in cell.contentView.subviews) {
        if ([view isKindOfClass:[UILabel class]]) {
            UILabel *lbl = (UILabel *)view;

            lbl.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
            lbl.textColor=[UIColor whiteColor];
            lbl.font=[UIFont fontWithName:@"HelveticaNeue" size:26];
            lbl.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
        }
    }
}
like image 33
Janmenjaya Avatar answered Oct 20 '22 01:10

Janmenjaya


Instead of configuring cell in cellForRowAtIndexPath, it's better if you use custom cell. Add imageView & separatorLineView in your Storyboard/ Nib itself. This way all cells are generated with these default properties. Also if you need to configure something through code, you can code it in your CustomCell.m file like this:

 class CustomCell: UITableViewCell {
    override func awakeFromNib() {
    super.awakeFromNib()
    self.textLabel.backgroundColor = UIColor.redColor
    //do other configurations
    }

Edit: Downloading images from web may be the reason for taking long time loading cells. Try downloading images asynchronously. You can also use this library: SDWebImage

Note: I know you want it in Objective C, above code in Swift is just for illustration.

like image 28
Arpit Dongre Avatar answered Oct 20 '22 00:10

Arpit Dongre