hahv
hahv

Reputation: 582

Make a Custom view that has a UIImage with a UILabel inside (at bottom of image)

An custom view with UIImage that have UILabel inside

I try to make a cool news app like BBC news. I think each news item (see picture) must have a UIImage and UILabel (title of news) inside it --> We must create custom view.

I adready have read how to create custom view in Apple Developer website but it only introduces, not has some samples.

My question is: + How can I create that customview (a UIImage with a UILabel inside) + How can I put that view in tableview in my main screen app.

Thank in advance. Sorry for poor English.

Upvotes: 2

Views: 6573

Answers (3)

Just Variable
Just Variable

Reputation: 877

Apart from @Shaheen M Basheer answer u can also try MGBox2 open source library which helps you to create Custom UIViews and arrange them in different Layout,also checkout sample given with it.It also support Grid layout which you can use to get what u intend.

Upvotes: 0

Shaheen M Basheer
Shaheen M Basheer

Reputation: 1010

You can accomplish this task in many ways

1.You can create a Custom View with UIImageView and UILabel and add it as subview in tableViewCell

2.You can create Custom TableViewCell with the required labels and UIImageView and use it directly.

To create Custom View with UIImageView and UILabel

Right click "project" -> Choose "New File" -> Select "Objective C Class"-> Select "Subclass of UIView"

CustomView.h

@interface CustomView : UIView
{
}
- (id)initWithFrame:(CGRect)frame withImage:(UIImage *)img withLabel:(NSString *)lbl ;
@end

CustomView.m

@implementation CustomView

- (id)initWithFrame:(CGRect)frame withImage:(UIImage *)img withLabel:(NSString *)lbl 
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        UIImageView * imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height - 30)];
        [imageView1 setImage:img];
        [self addSubview:imageView1];
        [imageView1 release];

        UILabel * label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, frame.size.height - 30, frame.size.width,30)];
        label1.text = lbl;
        label1.textColor = [UIColor blackColor];
        [self addSubview:label1];
        [label1 release];
    }
    return self;
}

@end

To use it

Import CustomView using #import "CustomView.h" and

CustomView * cView = [[CustomView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) withImage:[UIImage imageNamed:@"img.png"] withLabel:@"Testasddddddddddddd"];
[self.view addSubview:cView];
[cView release];

Upvotes: 6

Muhammed Ayaz
Muhammed Ayaz

Reputation: 1398

Try this..

    UIButton *objBtn;

    objBtn = [[UIButton alloc] init];
    objBtn.frame = CGRectMake(x, y, W, H);
    [objBtn setBackgroundImage:[UIImage imageNamed:@"defalt_person_01.png"] forState:UIControlStateNormal];
    [objBtn addTarget:self action:@selector(SelectLanguageBtn:) forControlEvents:UIControlEventTouchUpInside];
    objBtn.tag = 101;
    [self.view addSubview:objBtn];

    //Your News On Imagebtn
    UILabel *newsLab=[[UILabel alloc]initWithFrame:CGRectMake(BtnX, BtnY, W, H)];
    newsLab.textAlignment=UITextAlignmentCenter;
    newsLab.textColor=[UIColor orangeColor];


    newsLab.backgroundColor=[UIColor clearColor];

    newsLab.text = @"My News";

    newsLab.font=[UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:30];
    [objBtn addSubview:newsLab];

Upvotes: 1

Related Questions