我尝试制作一个很酷的新闻应用,比如 BBC 新闻。我认为每个新闻项(见图)必须有一个 UIImage 和 UILabel(新闻标题)在里面--> 我们必须创建自定义视图。
我已经阅读了如何在 Apple Developer 网站上创建自定义视图,但它只是介绍,没有一些示例。
我的问题是: + 如何创建自定义视图(内部带有 UILabel 的 UIImage) + 如何将该视图放在主屏幕应用程序的 tableview 中。
预先感谢。抱歉英语不好。
我尝试制作一个很酷的新闻应用,比如 BBC 新闻。我认为每个新闻项(见图)必须有一个 UIImage 和 UILabel(新闻标题)在里面--> 我们必须创建自定义视图。
我已经阅读了如何在 Apple Developer 网站上创建自定义视图,但它只是介绍,没有一些示例。
我的问题是: + 如何创建自定义视图(内部带有 UILabel 的 UIImage) + 如何将该视图放在主屏幕应用程序的 tableview 中。
预先感谢。抱歉英语不好。
您可以通过多种方式完成此任务
1.您可以使用 UIImageView 和 UILabel 创建一个自定义视图并将其添加为 tableViewCell 中的子视图
2.您可以使用所需的标签和UIImageView创建自定义TableViewCell并直接使用它。
使用 UIImageView 和 UILabel 创建自定义视图
右击“项目”->选择“新建文件”->选择“Objective C Class”->选择“UIView的子类”
自定义视图.h
@interface CustomView : UIView
{
}
- (id)initWithFrame:(CGRect)frame withImage:(UIImage *)img withLabel:(NSString *)lbl ;
@end
自定义视图.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
使用它
#import "CustomView.h"
使用和 导入 CustomView
CustomView * cView = [[CustomView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) withImage:[UIImage imageNamed:@"img.png"] withLabel:@"Testasddddddddddddd"];
[self.view addSubview:cView];
[cView release];
试试这个..
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];
除了@Shaheen M Basheer 回答,您还可以尝试MGBox2开源库,它可以帮助您创建自定义 UIViews 并将它们排列在不同的布局中,还可以查看随附的示例。它还支持网格布局,您可以使用它来获得您想要的.