textview加載html代碼的時(shí)候可能會(huì)出現(xiàn)圖片過大歼争,沒有辦法控制缩挑。下面這種處理方法可以解決拿走不用謝。
直接把這段代碼拼接上就可以了@"<head><style>img{width:100px !important;height:auto}</style></head>"
如果需要修改其他的樣式湃交,根據(jù)此方法依次類推就可以的
項(xiàng)目中我是這么寫的
//獲取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;