[英]how to add a UILabel in UIImageView
i have big Image in my app. 我的应用程序中有大图片。 i create it dynamically and i want to add a UILabel on this image:
我动态创建它,我想在此图像上添加UILabel:
UIImageView *thumbnailImage;
thumbnailImage = [[UIImageView alloc] initWithFrame:CGRectMake(xImage, yImage, imageWidth, imageHeight)];
UILabel *lblTitle = [[UILabel alloc] init];
lblTitle.text = @"SomeText";
lblTitle.frame = CGRectMake(xTitle, yTitle , titleWidth ,titleHeight);
[thumbnailImage setContentMode:UIViewContentModeScaleAspectFill];
thumbnailImage.clipsToBounds = YES;
[thumbnailImage addSubview:lblTitle];
but the [thumbnailImage addSubview:UILabel];
但是
[thumbnailImage addSubview:UILabel];
code does not work. 代码不起作用。
any one have any idea? 任何人有任何想法吗?
I want to add some text on bottom of picture like news article. 我想在图片底部添加一些文本,例如新闻文章。
so I solve it with below code, if any one have a better idea please share it. 因此,我用下面的代码解决了这个问题,如果有人有更好的主意,请分享。
UIImageView *thumbnailImage;
thumbnailImage = [[UIImageView alloc] initWithFrame:CGRectMake(xImage, yImage, imageWidth, imageHeight)];
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(xImage,imageHeight - titleHeight , titleWidth , titleHeight )];
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(xTitle, yTitle , titleWidth ,titleHeight)];
titleView.alpha = 0.3;
[thumbnailImage setContentMode:UIViewContentModeScaleAspectFill];
thumbnailImage.clipsToBounds = YES;
[self.view addSubview:thumbnailImage];
[self.view addSubview:titleView];
lblTitle.textLabel.text = @"SomeText";
first I use below line of Code: 首先我使用下面的代码行:
[titleView addSubview:lblTitle]
but the alpha of titleView
impact lblTitle
and made it transparent. 但是
titleView
的alpha titleView
影响lblTitle
并使它透明。 so after delete this line lblTitle
Text appear clear and bright. 因此,删除此行后,
lblTitle
文本将显得清晰明亮。
thumbnailImage is a UIView, so you can add subviews to it.instead of writing the line thumbnailImage是一个UIView,因此您可以向其添加子视图。
[thumbnailImage addSubview:UILabel];
edit it with the following :: 使用以下::对其进行编辑:
[thumbnailImage addSubview:lblTitle];
This will work . 这将起作用。 :)
:)
It's better to initialize the label with it's frame. 最好使用框架来初始化标签。
You should add a text to the label 您应该在标签上添加文字
You should add the UILabel
object ( lblTitle
) to the imageview instead of the UILabel
type. 您应该将
UILabel
对象( lblTitle
)添加到imageview而不是UILabel
类型。
With these corrections, your code should look like this: 经过这些更正,您的代码应如下所示:
UIImageView *thumbnailImage;
thumbnailImage = [[UIImageView alloc] initWithFrame:CGRectMake(xImage, yImage, imageWidth, imageHeight)];
[thumbnailImage setContentMode:UIViewContentModeScaleAspectFill];
thumbnailImage.clipsToBounds = YES;
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(xTitle, yTitle , titleWidth ,titleHeight)];
lblTitle.textLabel.text = @"Your text";
[thumbnailImage addSubview:lblTitle];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.