-3
  NSString *city2=[[NSString alloc]init];
  city2=[NSString stringWithFormat:@"the city orlando"];
  NSString *trimmedString = [city2 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  NSLog(@"%@", trimmedString)

   trimmedString=[NSString stringWithFormat:@"%@, ",trimmedString];

   CGSize constraint4 = CGSizeMake(650, 2000.0f);

  CGSize size4 = [city2 sizeWithFont: [UIFont fontWithName:@"Helvetica" size:12] constrainedToSize:constraint4 lineBreakMode:UILineBreakModeWordWrap];
   city = (UILabel *)[cell viewWithTag:115];

   [city setNumberOfLines:0];
   city.frame = CGRectMake(150,codetype.frame.size.height+codetype.frame.origin.y, size4.width, size4.height);          
   [city setText: [NSString stringWithFormat:@"%@",trimmedString]];                

我只得到城市而不是奥兰多城市,这是我项目的一小部分

实际上我正在将字符串分配给标签。但我没有得到标签中的总字符串。这里 line3 是UILabel高于城市标签的另一个。

4

4 回答 4

0

你得到这个 ->城市......而不是这个 ->城市奥兰多,?只需增加size4.width你的UILabelin the city.frameline,就像这样size4.width + 100.0f。这是我的代码版本。

NSString *city2 = @"the city orlando";
NSString *trimmedString = [city2 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];                                                                                                 
NSLog(@"%@", trimmedString);

trimmedString=[NSString stringWithFormat:@"%@,",trimmedString];

CGSize constraint4 = CGSizeMake(650, 2000.0f);

CGSize size4 = [city2 sizeWithFont: [UIFont fontWithName:@"Helvetica" size:12] constrainedToSize:constraint4 lineBreakMode:UILineBreakModeWordWrap];
city = [[UILabel alloc] init]; //(UILabel *)[cell viewWithTag:115];

[city setNumberOfLines:0];
city.frame = CGRectMake(150, 150, size4.width + 100.0f, size4.height + 10.0f);


[city setText: [NSString stringWithFormat:@"%@",trimmedString]];  

[self.view addSubview:city];
于 2013-02-20T05:18:04.417 回答
0

整个字符串在标签中不可见,因为 的宽度UILabel小于字符串的宽度。所以增加UILabel代码的宽度就可以了。

替换此代码

city.frame = CGRectMake(150,codetype.frame.size.height+codetype.frame.origin.y, size4.width, size4.height);

经过

city.frame = CGRectMake(150,codetype.frame.size.height+codetype.frame.origin.y, size4.width+100, size4.height);
于 2013-02-20T05:21:18.683 回答
0

看起来city标签太小以适合文本。可能会有不同的问题,但是当您使用它进行计算时,city标签可能没有设置Helvetica 12size4。另请注意,您获得的是 city2 的大小,但将文本设置为 trimmedString。

CGSize size4 = [city2 sizeWithFont: [UIFont fontWithName:@"Helvetica" size:12] constrainedToSize:constraint4 lineBreakMode:UILineBreakModeWordWrap];

您可以通过以下方式查看

[city setColor:[UIColor redColor]];

看看会发生什么。

或者,检查它快速更换

CGSize size4 = [city2 sizeWithFont: [UIFont fontWithName:@"Helvetica" size:12] constrainedToSize:constraint4 lineBreakMode:UILineBreakModeWordWrap];
city = (UILabel *)[cell viewWithTag:115];

city = (UILabel *)[cell viewWithTag:115];
CGSize size4 = [trimmedString sizeWithFont: city.font constrainedToSize:constraint4 lineBreakMode:UILineBreakModeWordWrap];
于 2013-02-20T05:21:27.300 回答
0

这段代码有很多问题,但主要问题似乎在于您计算字符串的大小。你得到的大小,city2但随后将标签的文本设置为trimmingString. 此外,用于计算的字体可能与标签的字体不同。使它们相同。

这是您的代码,其中包含大量清理和修复:

NSString *city2 = @"the city orlando";
NSString *trimmedString = [city2 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"%@", trimmedString)

trimmedString = [NSString stringWithFormat:@"%@, ", trimmedString];

CGSize constraint4 = CGSizeMake(650, 2000.0f);

UIFont *font = [UIFont fontWithName:@"Helvetica" size:12];
CGSize size4 = [trimmedString sizeWithFont:font constrainedToSize:constraint4 lineBreakMode:NSLineBreakByWordWrapping];
city = (UILabel *)[cell viewWithTag:115];

city.font = font;
[city setNumberOfLines:0];
city.frame = CGRectMake(150, codetype.frame.size.height + codetype.frame.origin.y,  size4.width, size4.height);

[city setText:trimmedString];

请避免对 的所有额外调用stringWithFormat:

于 2013-02-20T05:27:56.503 回答