如何滚动UIlabel Text并在目标c中动态设置它的宽度和高度

时间:2016-10-26 09:47:47

标签: html ios objective-c uiscrollview uilabel

我是iOS的新手,我在滚动标签文本时遇到问题。我的代码能够滚动标签文本,但宽度设置不正确。代码就像这样

 NSString * htmlString = @"<html><body>";
    NSString *htmlString2=@"</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 *htmlString2=@"</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);

我正在使用这个代码,但它的宽度不是动态设置的。如何动态设置宽度。在此先感谢!

1 个答案:

答案 0 :(得分:0)

请使用UITextView代替UILabel。 您的问题将以最少的代码行解决。