我正在寫一個基於幾何的應用程序。在應用程序的某一點,將會有一個UITableView與一些自定義單元格。這些單元格包含UILabels。在一些這些標籤的文本中,我想插入看起來這兩個三角形的符號:如何在UILabel中插入圖像,或使其看起來像我一樣?
但是,由於我無法在任何Apple字體中找到這些符號,是否有將圖像插入字符串的方法的符號?
這裏是一個(非常)什麼我要去我的(實際的表不會是一成不變的)的粗略的想法:
我正在寫一個基於幾何的應用程序。在應用程序的某一點,將會有一個UITableView與一些自定義單元格。這些單元格包含UILabels。在一些這些標籤的文本中,我想插入看起來這兩個三角形的符號:如何在UILabel中插入圖像,或使其看起來像我一樣?
但是,由於我無法在任何Apple字體中找到這些符號,是否有將圖像插入字符串的方法的符號?
這裏是一個(非常)什麼我要去我的(實際的表不會是一成不變的)的粗略的想法:
好吧,我得到你想要做的。我認爲,關鍵在於不斷添加控制到你的單元格,隨着時間的推移計算寬度。
首先,我會建議一個數據結構來保存您的單元格內容。一個簡單的數組將完成這項工作。我通常做這個伊娃的東西:
@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;
}
這產生了:
使用一些幾何邏輯有關圖像視圖放置字母不分大小的矩形...如
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);
寫這些得真快作爲一個概念證明...隨時修改,等等。
是否有創建如此之多的UILabel的選擇?這似乎是非常低效的,特別是在UITableViewCell中。 – Garrett 2012-07-09 18:20:04
如果你真的想要他們在一個表視圖,我會說你需要使用這些形狀預渲染的字母圖像... – 2012-07-09 18:24:36
如果你能以某種方式與表視圖外的這些工作,UILabel是根據我的理解,它是一個非常輕量級的UIKit元素,所以我不擔心它。我唯一的擔心是tableview出隊單元會導致很多問題聽到,尤其是不同的形狀和標籤配置 – 2012-07-09 18:25:32
你可以用你所需要的確切符號創建自己的字體。試試這個: http://glyphdesigner.71squared.com
太棒了!這正是我期待的答案。只有一個問題,我輸入的圖像太大了。你製作圖像的尺寸是多少? – Garrett 2012-07-09 21:33:52
沒關係我知道了 – Garrett 2012-07-09 21:52:37