3

ジオメトリベースのアプリを書いています。アプリのある時点で、いくつかのカスタムセルを含むUITableViewがあります。これらのセルにはUILabelが含まれています。これらのラベルのテキストの中に、これら2つの三角形に見える記号を挿入したいと思います:(出典:wiley.com画像

しかし、これらの記号はどのAppleフォントでも見つからないので、記号の代わりに画像を文字列に挿入する方法はありますか?

これが私が何をしようとしているのか(非常に)大まかな考えです(実際のテーブルは静的ではありません):

image2

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
  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 に答える
2

必要な記号を使用して独自のフォントを作成できます。これを試してください:http: //glyphdesigner.71squared.com

于 2012-07-09T20:38:33.243 に答える
0

UILabelに画像を挿入することはできません。ただし、UITableViewのカスタムセルにUIImageViewを追加してから、UIImageをその中に配置することができます。

于 2012-07-09T17:52:41.990 に答える