如何在ios中的自定义uitableviewcell中的图像上添加文本标签

时间:2014-10-16 12:54:23

标签: ios objective-c uitableview

大家好,            我是iOS开发的新手。现在我正在使用自定义tableView,在此我使用自定义tableViewCell。在那个单元格上,我在imageView上添加了文本标签。但它没有在图像视图上显示文本标签。只显示图像。    如果有人对此问题有任何解决方案,请帮助我。

这是我的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCell";
    ItemsCellVC *cell = (ItemsCellVC *)[TableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[ItemsCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];


    }

    UIImage *background = [self cellBackgroundForRowAtIndexPath:indexPath];
    UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
    cellBackgroundView.image = background;
    cell.backgroundView = cellBackgroundView;





    if ([arrayForItemDictionary count]!=0) {

    if (tableView == aSearchDisplayController.searchResultsTableView)
      {
        dictItemFromDatabase=[filteredListContent objectAtIndex:indexPath.row];
      }
    else
       {
        dictItemFromDatabase=[arrayForItemDictionary objectAtIndex:indexPath.row];
       }

   }




    //cell.containerNameLabel.text=[dictItemFromDatabase valueForKey:@"PickerName"];
    cell.itemNameLabel.text = [dictItemFromDatabase valueForKey:@"ItemNameTF"];
    cell.containerImgView.image = [UIImage imageNamed:@"12-02 (1)"];

  UIImage *image = [UIImage imageNamed:@"2-02 (1).png"];

   cell.containerImgView.image = [self addText:image text:@"Hello there"];



    return cell;
}


-(UIImage *)addText:(UIImage *)img text:(NSString *)text1{

    int w = img.size.width;
    int h = img.size.height;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);

    char* text= (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
    CGContextSelectFont(context, "Arial",20, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextSetRGBFillColor(context, 0, 0, 0, 1);
    CGContextShowTextAtPoint(context,10,10,text, strlen(text));
    CGImageRef imgCombined = CGBitmapContextCreateImage(context);

    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    UIImage *retImage = [UIImage imageWithCGImage:imgCombined];
    CGImageRelease(imgCombined);

    return retImage;
}

1 个答案:

答案 0 :(得分:-1)

经过努力,我得到了这个解决方案:

我在tableViewCellVC.m文件中使用了这个方法

@implementation UIImage (DrawWatermarkText)
-(UIImage*)drawWatermarkText:(NSString*)text {
 UIColor *textColor = [UIColor grayColor];//[UIColor colorWithWhite:0 alpha:0.1];
UIFont *font = [UIFont systemFontOfSize:400];
//  CGFloat paddingX = 50.f;
CGFloat paddingY = 200.f;

// Compute rect to draw the text inside
CGSize imageSize = self.size;
NSDictionary *attr = @{NSForegroundColorAttributeName: textColor, NSFontAttributeName: font};
CGSize textSize = [text sizeWithAttributes:attr];

// CGRect textRect = CGRectMake(imageSize.width - textSize.width - paddingX, imageSize.height - textSize.height - paddingY-50, textSize.width, textSize.height);

CGRect textRect = CGRectMake(imageSize.width - 1900, imageSize.height - textSize.height - paddingY, textSize.width, textSize.height);

// Create the image
UIGraphicsBeginImageContext(imageSize);
[self drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
[text drawInRect:CGRectIntegral(textRect) withAttributes:attr];
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;}@end

later on i had write following code in tableViewCellVC.h file as

@interface UIImage (DrawWatermarkText)
-(UIImage*)drawWatermarkText:(NSString*)text;
  @end
And Finally in cellforRowAtIndexPath in tavelVC.m call this method only

 UIImage *image = [UIImage imageNamed:@"12-02 (1)"];

cell.containerImgView.image  = [image drawWatermarkText:@"Bharat"];

然后它的作品