2012-07-09 81 views
3

我正在寫一個基於幾何的應用程序。在應用程序的某一點,將會有一個UITableView與一些自定義單元格。這些單元格包含UILabels。在一些這些標籤的文本中,我想插入看起來這兩個三角形的符號:image如何在UILabel中插入圖像,或使其看起來像我一樣?

但是,由於我無法在任何Apple字體中找到這些符號,是否有將圖像插入字符串的方法的符號?

這裏是一個(非常)什麼我要去我的(實際的表不會是一成不變的)的粗略的想法:

image2

回答

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

+0

太棒了!這正是我期待的答案。只有一個問題,我輸入的圖像太大了。你製作圖像的尺寸是多少? – Garrett 2012-07-09 21:33:52

+0

沒關係我知道了 – Garrett 2012-07-09 21:52:37

0

不能在插入圖片一個UILabel。 但是你可以在你的UITableView的自定義單元格中添加一個UIImageView,然後在裏面放置一個UIImage。

+0

是的,我很確定這將是答案。但是,如何將我的文字包裹在插入的圖像上? – Garrett 2012-07-09 17:54:24

+1

@Garrett如果你想要文字環繞圖像,你可能想要調查轉移到UIWebView,在那裏你以編程方式創建HTML。 – Rob 2012-07-09 17:58:10

+0

是的,我認爲這是更好的方法。 – 2012-07-09 18:00:14

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); 
    

寫這些得真快作爲一個概念證明...隨時修改,等等。

+0

是否有創建如此之多的UILabel的選擇?這似乎是非常低效的,特別是在UITableViewCell中。 – Garrett 2012-07-09 18:20:04

+0

如果你真的想要他們在一個表視圖,我會說你需要使用這些形狀預渲染的字母圖像... – 2012-07-09 18:24:36

+0

如果你能以某種方式與表視圖外的這些工作,UILabel是根據我的理解,它是一個非常輕量級的UIKit元素,所以我不擔心它。我唯一的擔心是tableview出隊單元會導致很多問題聽到,尤其是不同的形狀和標籤配置 – 2012-07-09 18:25:32

2

你可以用你所需要的確切符號創建自己的字體。試試這個: http://glyphdesigner.71squared.com

+0

這看起來不錯!如果我可以設定兩個正確的答案,我也會選擇這個答案。如果不是價格標籤,這絕對是我的首選。 – Garrett 2012-07-09 21:38:18

+0

此刻50%優惠! – jowie 2014-08-28 09:28:31

+0

實際上,我回來了...字形設計器是爲位圖字體,即位圖遊戲,如精靈表。它們與本機iOS文本組件不兼容,無法作爲字體加載! – jowie 2014-08-29 12:47:01

相關問題