应对富文本需求
富文本是一段有属性的字符串,可以包含不同字体,不同字号,不同背景,不同颜色,不同字间距的文字,还可以设置段落,图文混排等等属性。
WebView
1 2 3 4 5 6 7 8 9 10 11 12 13
| self.wbView = [[UIWebView alloc] init]; self.wbView.delegate = self; [self.view addSubview:self.wbView]; [self.wbView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.left.right.bottom.equalTo(self.view); }]; self.wbView.scalesPageToFit = YES; self.wbView.scrollView.directionalLockEnabled = YES; self.wbView.scrollView.showsHorizontalScrollIndicator = NO; [self.wbView setOpaque:NO];
[self.wbView loadHTMLString:articleString baseURL:nil];
|
在Cocoa层使用NSURLProtocol可以拦截所有HTTP的请求,可以以此来缓存文章中的图片。
YYText
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| NSMutableAttributedString *text = [NSMutableAttributedString new]; UIFont *font = [UIFont systemFontOfSize:16]; NSMutableAttributedString *attachment = nil;
UIImage *image = [UIImage imageNamed:@"dribbble64_imageio"]; attachment = [NSMutableAttributedString yy_attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:font alignment:YYTextVerticalAlignmentCenter]; [text appendAttributedString: attachment];
UISwitch *switcher = [UISwitch new]; [switcher sizeToFit]; attachment = [NSMutableAttributedString yy_attachmentStringWithContent:switcher contentMode:UIViewContentModeBottom attachmentSize:switcher.size alignToFont:font alignment:YYTextVerticalAlignmentCenter]; [text appendAttributedString: attachment];
CASharpLayer *layer = [CASharpLayer layer]; layer.path = ... attachment = [NSMutableAttributedString yy_attachmentStringWithContent:layer contentMode:UIViewContentModeBottom attachmentSize:switcher.size alignToFont:font alignment:YYTextVerticalAlignmentCenter]; [text appendAttributedString: attachment];
|