2016-10-26 3 views
0

저는 iOS에서 새롭고 라벨 텍스트를 스크롤하는 데 문제가 있습니다. 내 코드는 라벨 텍스트를 스크롤 할 수 있지만 너비는 설정되어 있지 않습니다. 정확히. 코드는 다음과 같습니다.UIlabel 텍스트를 스크롤하여 대상체 C에서 너비와 높이를 동적으로 설정하는 방법

NSString * htmlString = @"<html><body>"; 
    NSString *[email protected]"</body></html>"; 
    NewString=[NSString stringWithFormat:@"%@%@%@",htmlString,result,htmlString2]; 

    NSLog(@"New String =%@",NewString); 

    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[NewString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil]; 



    NSLog(@"String Value = %@",result); 
    // [myDataNSMArray addObject:idarray]; 

    shortnamearray=[[NSMutableArray alloc]init]; 
    shortnamearray=[responsedict valueForKey:@"abc"]; 

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    { 
     lbl = [[UILabel alloc]initWithFrame:CGRectMake(0,0,700, 1800)]; 
     NSString *theText = @"A long string"; 
     CGRect labelRect = CGRectMake(10, 50, 300, 50); 
     lbl.adjustsFontSizeToFitWidth = YES; 
     [lbl setNumberOfLines:0]; 
     CGFloat fontSize = 30; 
     while (fontSize > 0.0) 
     { 
      CGSize size = [theText sizeWithFont:[UIFont fontWithName:@"Verdana" size:fontSize] constrainedToSize:CGSizeMake(labelRect.size.width, 10000) lineBreakMode:NSLineBreakByWordWrapping]; 

      if (size.height <= labelRect.size.height) break; 

      fontSize -= 1.0; 
     } 

     //set font size 
     lbl.font = [UIFont fontWithName:@"Arial" size:fontSize]; 
    } 
    else 
    { 
     lbl = [[UILabel alloc]initWithFrame:CGRectMake(0,0,350, 800)]; 
    } 
    NSLog(@"Result Array =%@",shortnamearray); 
    CGFloat y = 10; 

    NSMutableArray* animals = [NSMutableArray new]; 

    NSUInteger maxCount = headarray.count > shortnamearray.count ? headarray.count : shortnamearray.count; 
    for (int i = 0; i < maxCount; i ++) { 
     if ([headarray objectAtIndex:i]) { 
      [animals addObject:[headarray objectAtIndex:i]]; 
     } 
     if ([shortnamearray objectAtIndex:i]) { 
      [animals addObject:[shortnamearray objectAtIndex:i]]; 
     } 
    } 
    NSLog(@"Array is =%@",animals); 

    for(int i=0;i<[shortnamearray count] && i<[headarray count];i++){ 
     // y+=20; 
     y+=10; 

     NSString *newArray =[animals objectAtIndex:i]; 
     newArray=[animals componentsJoinedByString:@""]; 

     NSString *NewString; 

     [lbl setLineBreakMode:NSLineBreakByWordWrapping]; 
     lbl.textAlignment = NSTextAlignmentCenter; 
     NSString * htmlString = @"<html><body>"; 
     NSString *[email protected]"</body></html>"; 
     NewString=[NSString stringWithFormat:@"%@%@%@",htmlString,newArray,htmlString2]; 

     NSLog(@"New String =%@",NewString); 

     NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[NewString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil]; 

     lbl.attributedText = attrStr; 


     NSString *theText = @"A long string"; 
     CGRect labelRect = CGRectMake(10, 50, 300, 50); 
     lbl.adjustsFontSizeToFitWidth = YES; 
     [lbl setNumberOfLines:0]; 
     CGFloat fontSize = 15; 
     while (fontSize > 0.0) 
     { 
      CGSize size = [theText sizeWithFont:[UIFont fontWithName:@"Verdana" size:fontSize] constrainedToSize:CGSizeMake(labelRect.size.width, 10000) lineBreakMode:NSLineBreakByWordWrapping]; 

      if (size.height <= labelRect.size.height) break; 

      fontSize -= 1.0; 
     } 

     //set font size 
     lbl.font = [UIFont fontWithName:@"Arial" size:fontSize]; 
    } 

    NSTimer *timer = [NSTimer timerWithTimeInterval:1 
              target:self 
              selector:@selector(timer) 
              userInfo:nil 
              repeats:YES]; 
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; 

    //[_scrollView scrollRectToVisible:NSMakeRange(0,0,0.0) animated:YES]; 
    [scroll addSubview:lbl]; 
    [UIView commitAnimations]; 

    scroll.contentSize=CGSizeMake(scroll.frame.size.width+[shortnamearray count], lbl.frame.size.height); 

이 코드와 같이 사용하지만 동적으로 설정되지 않습니다. 동적으로 너비를 설정하는 법. 미리 감사드립니다.

+0

sizeToFit을 사용해 보셨습니까? – Joshua

+0

@ Joshua 그래 나는 그것을 시도했다. – Muju

+0

@Muju 'UILabel' 대신에'UITextView'를 사용하는 것이 좋습니다. 텍스트를 스크롤 할 수있게 해주고 HTML에서 속성이있는 텍스트는 하이퍼 링크를 클릭 할 수있게합니다. – kamwysoc

답변

0

UILabel 대신 UITextView를 사용하십시오. 문제는 최소 코드 줄에서 해결됩니다.

관련 문제