求助,关于图文混排。

发布于 2022-08-31 20:48:33 字数 3843 浏览 15 评论 0

最近项目里用到图文混排相关的,苦恼了好久没搞出来。没什么头绪,求方法求建议。
基本样式如下。
74681984gw1em4rss3ooqj20hs0vkq7m.jpg

段落中间会按照返回数据中的链接位置显示图片,图片大小不定,需要居中显示。
查了下的实现方法,NSAttributedStrings+自定义的NSTextAttachment可以排版,现在的问题是算的高度总是不对。排版的代码贴上,求建议。。以及遇到比较棘手的问题的时候,大家一般都是怎么解决的。。。
自定义的NSTextAttachment

- (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex {
CGFloat width = lineFrag.size.width;

// Scale how you want
float scalingFactor = 1.0;
CGRect rect;
CGSize imageSize = [self.image size];
if (width < imageSize.width) {
    scalingFactor = width / imageSize.width;
    rect = CGRectMake(0, 0, imageSize.width * scalingFactor, imageSize.height * scalingFactor);
}

return rect;

}

以及实现代码

NSString *s = @"Vintage Camera Day\n\nVintage Camera Day is a celebration of the history of the camera. In her time, Alice Austen used a glass plate camera, but like many photographers today, she followed the latest trends in camera and lens technology. This event will feature activities on vintage image-making, talks by artists and photographers, and displays of vintage cameras and equipment.\n[img]http://aliceausten.org/sites/default/files/styles/thumbnail/public/event/vcd.jpg[/img]\nActivities include:\n-Vintage camera show-and-tell (BYOC - bring your own camera)\n-Tours of Alice Austen’s darkroom\n-Display of one of Alice Austen’s original cameras\n-Dress-up for a silhouette photograph\n-View vintage photography and camera collections with artist Ciro Galeno (including ambrotypes, tintypes, daguerreotypes)\n-Learn about Alice Austen’s lenses and cameras (@1:30 p.m.) with Geoffrey Berliner, Executive Director of the Penumbra Foundation\n-Steampunk Photo Parlor Tricks magic lantern demonstration (@ 3:00 p.m.) with Edward Coppola and Doug Schwab";
s = [s stringByReplacingOccurrencesOfString:@"[img]" withString:@"\n"];
s = [s stringByReplacingOccurrencesOfString:@"[/img]" withString:@"\n"];


NSError *error;
NSString *regulaStr = @"\\bhttps?://[a-zA-Z0-9\\-.]+(?::(\\d+))?(?:(?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:&error];
NSArray *arrayOfAllMatches = [regex matchesInString:s options:0 range:NSMakeRange(0, [s length])];


NSMutableAttributedString * string = [[NSMutableAttributedString alloc ] initWithString:s  attributes:nil ];

__block NSInteger length = 0;
for (int i = 0; i < [arrayOfAllMatches count]; i++) {
    NSTextCheckingResult *match = arrayOfAllMatches[i];
    NSString* substringForMatch = [s substringWithRange:match.range];
    NSLog(@"substringForMatch  %@",substringForMatch);


    __block MMTextAttachment *textAttachment = [[MMTextAttachment alloc] initWithData:nil ofType:nil ] ;

    [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:substringForMatch] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {

    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        textAttachment.image = image;
        NSAttributedString * textAttachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment];
        [string replaceCharactersInRange:NSMakeRange(match.range.location + i - length, match.range.length) withAttributedString:textAttachmentString];
        length += match.range.length;
    }];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

热风软妹 2022-09-07 20:48:33

iOS7 后的Text kit可以实现图文混合排版,我也是了解,不会debug啊

深海蓝天 2022-09-07 20:48:33

我是可以做到显示没有问题,但是发送遇到了瓶颈,请问您最后解决了吗?

动次打次papapa 2022-09-07 20:48:33

唐巧的博客里有详细写这个的

人事已非 2022-09-07 20:48:33

MMTextAttachment能方便透露一下吗

一腔孤↑勇 2022-09-07 20:48:33

这个有bug 我试了 多图下载的话就会越界,有没有解决办法呢(textview里面要显示很多张图)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文