求助,关于图文混排。
最近项目里用到图文混排相关的,苦恼了好久没搞出来。没什么头绪,求方法求建议。
基本样式如下。
段落中间会按照返回数据中的链接位置显示图片,图片大小不定,需要居中显示。
查了下的实现方法,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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
iOS7 后的Text kit可以实现图文混合排版,我也是了解,不会debug啊
我是可以做到显示没有问题,但是发送遇到了瓶颈,请问您最后解决了吗?
唐巧的博客里有详细写这个的
MMTextAttachment能方便透露一下吗
这个有bug 我试了 多图下载的话就会越界,有没有解决办法呢(textview里面要显示很多张图)