如何在RTLabel中添加UIImageView?

时间:2014-07-04 12:08:54

标签: ios objective-c uiimageview uilabel

我正在为iPhone创建一个iOS文章阅读应用程序,其中包含多个标题,图像和多行文字。

我使用RTLabel来解析html数据,html包含图片。我将从url创建图像并在RTlabel中添加imageview,但图像不会显示在RTlabel中。

到目前为止我在哪里:

   label = [[RTLabel alloc] initWithFrame:CGRectMake(10, 270, 300, 1000)];
   [self.scroller addSubview:label];
    NSString *aString = [[NSString alloc] initWithString:_DetailModal1[3] ];
    NSString *newS=[aString stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *newString=  [newS stringByReplacingOccurrencesOfString:@"<br\/>" withString:@"<br>"];
   NSRange rangeOfString = NSMakeRange(0, [newString length]);
          NSString *pattern = @"src=\"([^\"]+)\"";
    NSError* error = nil;
    NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
    NSArray *matchs = [regex matchesInString:newString options:0 range:rangeOfString];
    for (NSTextCheckingResult* match in matchs) {
        NSLog(@"url: %@", [newString  substringWithRange:[match rangeAtIndex:1]]);
        NSString *imageUrl=[newString  substringWithRange:[match rangeAtIndex:1]];
        NSRange range = [newString rangeOfString:imageUrl];
        NSLog(@"%@",NSStringFromRange(range));
        if (range.location != NSNotFound) {
            UIImageView*    imgView = [[UIImageView alloc]init];
            [imgView setFrame:CGRectMake(0, range.location, 320, 160)];
        NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: imageUrl]];
            UIImage *image = [[UIImage alloc] initWithData:imageData];

            imgView.image=image;
            [label addSubview:imgView];  //adding imageview in the RTlabel

            }
        else {
            NSLog(@"not found");

        }}

1 个答案:

答案 0 :(得分:0)

(RTLabel)Rich text formatting based on HTML-like markups for iOS.

它不是为了显示解析的图像,你必须为你的目的尝试更多的手动格式化