2016-10-26 8 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 'UITextView'を' UILabel'の代わりに使うことをお勧めします。これはあなたがテキストをスクロールさせ、htmlの属性付きテキストでハイパーリンクをクリックできるようにします。 – kamwysoc

答えて

0

UILabelの代わりにUITextViewを使用してください。 問題は最小限のコード行で解決されます。

関連する問題