如何在UILabel中插入图像,或者让它看起来像我一样?

时间:2012-07-09 17:48:18

标签: iphone ios uilabel

我正在编写一个基于几何的应用程序。在应用程序的某一点,将有一个UITableView与一些自定义单元格。这些单元格包含UILabels。在某些标签的文本中,我想插入看起来这两个三角形的符号:image

但是,由于我无法在任何Apple字体中找到这些符号,有没有办法将图像插入字符串中代替符号?

这是一个( 非常 )关于我的目标的粗略概念(实际的表格不会是静态的):

image2

4 个答案:

答案 0 :(得分:4)

好的,我得到了你想做的事。我认为,关键是不断向细胞添加控制,随着时间的推移计算宽度。

首先,我建议一个数据结构来保存你的细胞内容。一个简单的数组将完成这项工作。我通常把这些东西当作伊娃做:

@interface LabelWithImagesViewController ()
{
    NSMutableArray *_cells;
}
@end

然后用您想要的文本和图像填充此数组。我正在做一行,但你可以重复你需要的每一行。

- (void)viewDidLoad
{
    [super viewDidLoad];

    _cells = [[NSMutableArray alloc] init];

    [_cells addObject:[[NSArray alloc] initWithObjects:
                       [UIImage imageNamed:@"triangle.png"],
                       @"CAT",
                       [UIImage imageNamed:@"semiequal.png"],
                       [UIImage imageNamed:@"triangle.png"],
                       @"DOG",
                       @"  If",
                       [UIImage imageNamed:@"triangle1.png"],
                       @"then",
                       [UIImage imageNamed:@"triangle2.png"],
                       nil]];
}

然后,您需要创建您的单元格:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return _cells.count;
}

#define kEquationTag 100
#define kCellHeight 44

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"equationCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UIView *equationContainer;

    if (cell == nil)
    {
        // if we don't have a cell create it, including the frame to hold our custom stuff

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        equationContainer = [[UIView alloc] initWithFrame:cell.contentView.bounds];
        equationContainer.tag = kEquationTag;
        [cell.contentView addSubview:equationContainer];
    }
    else
    {
        // if we are dequeing one that already exists, let's get rid of the old custom stuff

        equationContainer = [cell.contentView viewWithTag:kEquationTag];
        for (UIView *view in equationContainer.subviews)
        {
            [view removeFromSuperview];
        }
    }

    // Configure the cell...

    NSArray *cellContents = [_cells objectAtIndex:indexPath.row];

    NSUInteger x = 0;
    UIFont *font = [UIFont systemFontOfSize:12.0];

    for (NSObject *obj in cellContents)
    {
        if ([obj isKindOfClass:[NSString class]])
        {
            NSString *text = (NSString *)obj;
            CGSize size = [text sizeWithFont:font];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, (kCellHeight - size.height)/2.0, size.width, size.height)];
            label.text = text;
            label.font = font;
            [equationContainer addSubview:label];
            x += size.width;
        } 
        else if ([obj isKindOfClass:[UIImage class]])
        {
            UIImage *image = (UIImage *)obj;
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, (kCellHeight - image.size.height) / 2.0, image.size.width, image.size.height)];
            imageView.image = image;
            [equationContainer addSubview:imageView];
            x += image.size.width;
        }
    }

    return cell;
}

这会产生:

enter image description here

答案 1 :(得分:2)

  1. 制作UILabel,并为每个字母使用该UILabel的实例。
  2. 使用一些关于图像视图矩形的几何逻辑来放置字母而不管大小......例如

        UILabel *aLetterLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
        aLetterLabel.text = @"A";
    
        //centered top
        aLetterLabel.frame = CGRectMake((shapeImage.frame.origin.x +  (shapeImage.frame.size.width/2)), shapeImage.frame.origin.y, 35, 35);
    
        //centered
        aLetterLabel.frame = CGRectMake((shapeImage.frame.origin.x + (shapeImage.frame.size.width/2)), (shapeImage.frame.origin.y + (shapeImage.frame.size.height/2)), 35, 35);
    
        //cenetered bottom
        aLetterLabel.frame = CGRectMake((shapeImage.frame.origin.x + (shapeImage.frame.size.width/2)), (shapeImage.frame.origin.y+(shapeImage.frame.size.height-35)), 35, 35);
    
        //left center align
        aLetterLabel.frame = CGRectMake(shapeImage.frame.origin.x, (shapeImage.frame.origin.y + (shapeImage.frame.size.height/2)), 35, 35);
    
  3. 将这些写得非常快,作为概念证明......随意修改等等。

答案 2 :(得分:2)

您可以使用所需的确切符号创建自己的字体。试试这个: http://glyphdesigner.71squared.com

答案 3 :(得分:0)

您无法在UILabel中插入图像。 但您可以在UITableView的自定义单元格中添加UIImageView,然后将UIImage放入其中。