i have big Image in my app. i create it dynamically and i want to add a UILabel on this image:
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];
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. so after delete this line lblTitle
Text appear clear and bright.
thumbnailImage is a UIView, so you can add subviews to it.instead of writing the line
[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.
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];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.