gpt4 book ai didi

iphone - 创建一个自定义 View ,其中包含一个 UIImage,其中包含一个 UILabel(在图像底部)

转载 作者:行者123 更新时间:2023-12-03 20:25:18 42 4
gpt4 key购买 nike

An custom view with UIImage that have UILabel inside

我尝试制作一个像 BBC 新闻一样很酷的新闻应用程序。我认为每个新闻项(见图)里面必须有一个 UIImage 和 UILabel(新闻标题) --> 我们必须创建自定义 View 。

我已经在Apple Developer网站上阅读了如何创建自定义 View ,但它只是介绍,没有一些示例。

我的问题是:+ 如何创建自定义 View (内部带有 UILabel 的 UIImage)+ 如何将该 View 放入主屏幕应用程序的表格 View 中。

提前致谢。抱歉英语不好。

最佳答案

您可以通过多种方式完成此任务

1.您可以使用 UIImageView 和 UILabel 创建自定义 View ,并将其添加为 tableViewCell 中的 subview

2.您可以使用所需的标签和UIImageView创建自定义TableViewCell并直接使用。

使用 UIImageView 和 UILabel 创建自定义 View

右键单击“项目”->选择“新建文件”->选择“Objective C Class”->选择“UIView的子类”

自定义 View .h

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

自定义 View .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];

关于iphone - 创建一个自定义 View ,其中包含一个 UIImage,其中包含一个 UILabel(在图像底部),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10274859/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com