xcode 制作一个带有 UIImage 的自定义视图,里面有一个 UILabel(在图像底部)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10274859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make a Custom view that has a UIImage with a UILabel inside (at bottom of image)
提问by Hoang Van Ha
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.
我尝试制作一个很酷的新闻应用,比如 BBC 新闻。我认为每个新闻项目(见图片)里面必须有一个 UIImage 和 UILabel(新闻标题)--> 我们必须创建自定义视图。
I adready have read how to create custom view in Apple Developer website but it only introduces, not has some samples.
我已经阅读了如何在 Apple Developer 网站中创建自定义视图,但它只是介绍,没有一些示例。
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.
我的问题是: + 如何创建自定义视图(内部带有 UILabel 的 UIImage) + 如何将该视图放入主屏幕应用程序的 tableview 中。
Thank in advance. Sorry for poor English.
预先感谢。抱歉英语不好。
采纳答案by Ayaz
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];
回答by Shaheen M Basheer
You can accomplish this task in many ways
您可以通过多种方式完成此任务
1.You can create a Custom Viewwith UIImageView and UILabel and add it as subview in tableViewCell
1.您可以使用 UIImageView 和 UILabel创建自定义视图并将其添加为 tableViewCell 中的子视图
2.You can create Custom TableViewCellwith the required labels and UIImageView and use it directly.
2.您可以使用所需的标签和 UIImageView创建自定义 TableViewCell并直接使用它。
To create Custom View with UIImageView and UILabel
使用 UIImageView 和 UILabel 创建自定义视图
Right click "project" -> Choose "New File" -> Select "Objective C Class"-> Select "Subclass of UIView"
右键单击“项目”-> 选择“新建文件”-> 选择“Objective C 类”-> 选择“UIView 的子类”
CustomView.h
自定义视图.h
@interface CustomView : UIView
{
}
- (id)initWithFrame:(CGRect)frame withImage:(UIImage *)img withLabel:(NSString *)lbl ;
@end
CustomView.m
自定义视图.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
使用#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];
回答by Deepak Samuel Rajan
Apart from @Shaheen M Basheer answer u can also try MGBox2open 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.
除了@Shaheen M Basheer 的回答,您还可以尝试MGBox2开源库,它可以帮助您创建自定义 UIViews 并将它们排列在不同的布局中,还可以使用它提供的结帐示例。它还支持网格布局,您可以使用它来获得您想要的.