由于之前的人寫的排版有問題斟赚,每串字符串只提取了一張圖片着降,圖片大小不對,所以需要更改一下拗军,我就不多說人家怎么寫的了任洞,一句話就是:脫褲子放屁蓄喇。我們的系統(tǒng)自帶控件UILabel,UITextView是可以直接展示html字符的交掏,但是我們的圖片有的需要放大妆偏,接下來最重要的是拿出圖片進(jìn)行放大操作。
開始我也考慮多了盅弛,準(zhǔn)備用coreText實(shí)現(xiàn)钱骂,但是我感覺對這種代碼接入需要不少時(shí)間,本身也不太熟悉挪鹏,然后就找到了這個(gè)簡單的方法见秽,其實(shí)用UILabel也是可以實(shí)現(xiàn)的,稍微麻煩一點(diǎn).
拿到的標(biāo)簽字符串如下格式
<p>背景:</p><p>某單項(xiàng)工程,按如下進(jìn)度計(jì)劃網(wǎng)絡(luò)圖組織施工:</p><p>
</p><p></p><p>1499044337002051156.png
</p><p>原計(jì)劃工期是170d,在第75d進(jìn)行進(jìn)度檢查時(shí)發(fā)現(xiàn):工作A已全部完成,工作B剛剛開工讨盒。由于工作B是關(guān)鍵工作,所以它拖后15d,將導(dǎo)致總工期延長15d解取。</p><p>
</p><p>為使本單項(xiàng)工程仍按原工期完成,必須趕工,調(diào)整原計(jì)劃后本工程各工作相關(guān)參數(shù)見下表:</p><p>
</p><p></p><p>1499044357339001366.png
</p><p> 問題:</p><p>
</p><p>1. 應(yīng)如何調(diào)整原計(jì)劃,既經(jīng)濟(jì)又保證整體工作能在計(jì)劃的170d內(nèi)完成,并列出詳細(xì)調(diào)整過程。</p><p>
</p><p>2. 試計(jì)算經(jīng)調(diào)整后,所需投入的趕工費(fèi)用返顺。</p><p>
</p><p>3. 重新繪制調(diào)整后的進(jìn)度計(jì)劃網(wǎng)絡(luò)圖,并列出關(guān)鍵線路(工作表示)禀苦。</p>
創(chuàng)建富文本字符串,一般為可變的,便于做其它操作
NSMutableAttributedString * mutableAttributedString = [[NSMutableAttributedString alloc] initWithData:[questionTitleStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSFontAttributeName : [UIFont systemFontOfSize:15] } documentAttributes:nil error:nil];
修改富文本中的圖片顯示尺寸
[mutableAttributedString enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, attrStr.length) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
if (value && [value isKindOfClass:[NSTextAttachment class]]) {
NSTextAttachment *textAttachment = value;
CGFloat width = CGRectGetWidth(textAttachment.bounds);
CGFloat height = CGRectGetHeight(textAttachment.bounds);
if (width > K_SCREEN_WIDTH) {// 大于屏幕寬度時(shí)遂鹊,縮小bounds寬度振乏,高度
height = (K_SCREEN_WIDTH - 20) / width * height;
width = K_SCREEN_WIDTH - 20;
textAttachment.bounds = CGRectMake(0, 0, width, height);
}
}
}];
初始化UITextView對象
// 初始化UITextView ,承載富文本內(nèi)容
UITextView *questionTextView = [[UITextView alloc] initWithFrame:CGRectMake(8, 8, K_SCREEN_WIDTH - 8*2, 0)];
questionTextView.attributedText = mutableAttributedString;// 添加富文本內(nèi)容
questionTextView.backgroundColor = [UIColor whiteColor];
questionTextView.userInteractionEnabled = YES;
questionTextView.editable = NO;// 不需要編輯
questionTextView.scrollEnabled = NO; // 不滾動(dòng)
questionTextView.delegate = self;
[supView addSubview:questionTextView];
實(shí)現(xiàn) UITextView 代理方法里拿到 textAttachment 對象
如果是自己添加進(jìn)去的textAttachment
,是可以直接拿到textAttachment.image
的秉扑,像我這里的數(shù)據(jù)格式是拿不到的慧邮,我們?nèi)ツ眠@個(gè) textAttachment.fileWrapper.regularFileContents
,得到的是一串data數(shù)據(jù)
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction NS_AVAILABLE_IOS(10_0){
if(textAttachment) {
EDPhotoBrowser *imageBrowser = [[EDPhotoBrowser alloc] init];
UIImage *image = [UIImage imageWithData:textAttachment.fileWrapper.regularFileContents];
[imageBrowser showLargePicture:image];
}
return YES;
}