2014-12-12 8 views
0

최소 iOS 7 버전의 앱을 구축하고 있습니다.이 코드에서는 을 다른 UIFonts 유형으로 그리는 코드가 있습니다.다른 글꼴 유형을 사용하는 UILabel의 크기를 계산하십시오.

//Get Strings 
NSString* author = [self getUserCreatedVideo:notf]; 
NSString* caption = [NSString stringWithFormat:@"\"%@\"", [notf objectForKey:@"title"]]; 
NSString* challenge = @"challenged you to reply to"; 
NSString* timeToReply = @"24h to reply"; 
NSString* myString = [NSString stringWithFormat:@"%@ %@ %@ %@", author, challenge, caption, timeToReply]; 

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:myString]; 
UIColor* greenColor = [UIColor colorWithRed:102.0/255.0 green:204.0/255.0 blue:153.0/255.0 alpha:1]; 

//Author 
[str addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(0,author.length)]; 
[str addAttribute:NSFontAttributeName value: [UIFont fontWithName:@"Helvetica" size:17.0] range:NSMakeRange(0,author.length)]; 
//Challenge 
[str addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(author.length+1,challenge.length)]; 
[str addAttribute:NSFontAttributeName value: [UIFont fontWithName:@"Helvetica" size:13.0] range:NSMakeRange(author.length+1,challenge.length)]; 
//Caption 
[str addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(author.length+1+challenge.length+1,caption.length)]; 
[str addAttribute:NSFontAttributeName value: [UIFont fontWithName:@"Helvetica" size:14.0] range:NSMakeRange(author.length+1+challenge.length+1,caption.length)]; 
//Time To Reply 
[str addAttribute:NSForegroundColorAttributeName value:greenColor range:NSMakeRange(author.length+1+challenge.length+1+caption.length+1,timeToReply.length)]; 
[str addAttribute:NSFontAttributeName value: [UIFont fontWithName:@"Helvetica" size:14.0] range:NSMakeRange(author.length+1+challenge.length+1+caption.length+1,4)]; 

cell.subtext.attributedText = str; 

지금 내가 cell.subtext 아래에 UILabel 20 픽셀을 그릴 수 있도록 UILabel 텍스트 크기를 계산합니다. 내가 어떻게 이걸 얻을 수 있니?

답변

1

기본적으로 텍스트 너비를 알 때 텍스트 높이를 계산하려고합니다.

CGRect rect = [str boundingRectWithSize:CGSizeMake(cellWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil]; 

원하는대로 rect.size를 사용하십시오.

+0

네, 이것이 내가 원하는 것입니다. 나의 지식 부족으로 나는 이것을 묻게했다. – bruno