我正在编写一个基于几何的应用程序。在应用程序的某一点,将有一个UITableView与一些自定义单元格。这些单元格包含UILabels。在某些标签的文本中,我想插入看起来这两个三角形的符号:
但是,由于我无法在任何Apple字体中找到这些符号,有没有办法将图像插入字符串中代替符号?
这是一个( 非常 )关于我的目标的粗略概念(实际的表格不会是静态的):
答案 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;
}
这会产生:
答案 1 :(得分: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);
将这些写得非常快,作为概念证明......随意修改等等。
答案 2 :(得分:2)
您可以使用所需的确切符号创建自己的字体。试试这个: http://glyphdesigner.71squared.com
答案 3 :(得分:0)
您无法在UILabel中插入图像。 但您可以在UITableView的自定义单元格中添加UIImageView,然后将UIImage放入其中。