Scrollview中的动态标签

时间:2013-03-28 12:21:41

标签: iphone objective-c xcode uiscrollview uilabel

我需要对我的项目提供帮助。 我想实现以下目标:

  1. 我有两个数组

    NSMutableArray *languageArray=[[NSMutableArray alloc]initWithObjects:@"Chinese bla blub",@"Spanish",@"English",@"Arabic",@"Hindi",@"Bengali",@"Portuguese",@"Russian",@"Japanee",@"German",@"German",@"German",@"German",@"German",nil];
    
    NSMutableArray *languageArray2=[[NSMutableArray alloc]initWithObjects:@"Hier kommt nun was ganz langes, jetzt mach ich das noch länger, damit der scheiß auch wirklich funktioniert.",@"Spanish jetzt hist hier auch mehr drin das sieht ja super aus wieviel bekomme ich den hier rein ",@"English",@"Arabic",@"Hindi",@"Bengali",@"Portuguese",@"Russian ajkslasdöadfnaklsdmnklsd",@"Japanese",@"German",@"German",@"German hier steht jetzt was ganz langes drin",@"German",@"German",nil];
    
  2. 我有UIScrollView

  3. 正如您所见,两个数组中的ObjectAtIndex:0内容和长度不同

  4. 我想为My Array中的每个Object设置一个标签,用于阵列1,屏幕的左侧站点,另一个标签从标签1的末尾开始。

  5. 喜欢这张照片。 See this link 我怎么做? 有一个简单的方法吗? 感谢

1 个答案:

答案 0 :(得分:0)

您可以使用此方法将动态高度设置为UILable ...

-(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width :(UILineBreakMode)lineBreakMode
{
[text retain];
[withFont retain];
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];

[text release];
[withFont release];

return suggestedSize.height;
}

并像下面那样使用它。

UILabel *lblAddress = [[UILabel alloc]init];
[lblAddress setFrame:CGRectMake(110, 31, 200, 50)];        
lblAddress.text = @"your Text ";
lblAddress.lineBreakMode = UILineBreakModeWordWrap;
lblAddress.numberOfLines = 0;
lblAddress.font = [UIFont fontWithName:@"Helvetica" size:12];

lblAddress.frame = CGRectMake(lblAddress.frame.origin.x, lblAddress.frame.origin.y, 
                         200,[self calculateHeightOfTextFromWidth:lblAddress.text :lblAddress.font :200 :UILineBreakModeWordWrap] ); 

lblAddress.textColor = [UIColor darkGrayColor];

[self.view addSubview:lblAddress];

然后设置UIScrollView's内容大小,如下所示..

float fscrview = lblAddress.frame.origin.y + lblAddress.frame.size.height + 20;
yourScrollView.contentSize=CGSizeMake(320, fscrview);

我希望这对你有帮助......