textview加載html代碼的時(shí)候可能會(huì)出現(xiàn)圖片過(guò)大,沒(méi)有辦法控制。下面這種處理方法可以解決拿走不用謝。
直接把這段代碼拼接上就可以了@"<head><style>img{width:100px !important;height:auto}</style></head>"
如果需要修改其他的樣式,根據(jù)此方法依次類(lèi)推就可以的
項(xiàng)目中我是這么寫(xiě)的
//獲取textview寬度:contentTextView.width
//后臺(tái)返回的html字符串:text
//顯示到textview中:contentTextView.attributedText=attribute
NSString *str = [NSStringstringWithFormat:@"<head><style>img{width:%f !important;height:auto}</style></head>%@",contentTextView.width,text];
NSAttributedString *attributedString = [[NSAttributedStringalloc] initWithData:[strdataUsingEncoding:NSUnicodeStringEncoding]options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }documentAttributes:nilerror:nil];
contentTextView.attributedText=attributedString;