3

我正在编写一个基于几何的应用程序。在应用程序的某一时刻,会有一个带有一些自定义单元格的 UITableView。这些单元格包含 UILabel。在这些标签的文本中,我想插入看起来像这两个三角形的符号:(来源:wiley.com图片

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

这是我要做什么的(非常)粗略的想法(实际的表不会是静态的):

图2

4

4 回答 4

4

好的,我明白你想要做什么。我认为,关键是继续向单元格添加控件,并在进行时计算宽度。

首先,我建议使用一种数据结构来保存您的单元格内容。一个简单的数组就可以完成这项工作。我通常将这些东西作为 ivar:

@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;
}

这产生:

在此处输入图像描述

于 2012-07-09T20:29:20.873 回答
2

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

于 2012-07-09T20:38:33.243 回答
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);
    

把这些写得非常快,作为概念证明……随时修改,等等。

于 2012-07-09T18:14:04.097 回答
0

您不能在 UILabel 中插入图像。但是你可以在你的 UITableView 的自定义单元格中添加一个 UIImageView ,然后在里面放一个 UIImage 。

于 2012-07-09T17:52:41.990 回答