问题
I am using a font in the same in attribute: , it is giving above error
do {
let myAttribute = [ NSAttributedStringKey.font: UIFont(name: "Chalkduster", size: 18.0)! ]
let attrStr = try NSAttributedString(data: ("Some String HTML".data(using: String.Encoding.unicode, allowLossyConversion: false)!)!,options: [.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil,attributes: myAttribute)
self.NoAccessMessage.font = UIFont(name: "Arial", size: 16.0)
self.NoAccessMessage.attributedText = attrStr
} catch let error {
}
Please check if any help is required
回答1:
You are combining two different things. You can either apply an attribute to a string or read some HTML. Not both.
Perhaps you mean this:
let myAttribute = [ NSAttributedStringKey.font: UIFont(name: "Chalkduster", size: 18.0)! ]
let attrStr = NSAttributedString(string: "howdy there", attributes: myAttribute)
Or perhaps you mean this:
let html = "<p>howdy <b>there</b></p>"
let htmldata = html.data(using: .utf8)
let attrStr = try NSAttributedString(data: htmldata!, options: [.documentType : NSAttributedString.DocumentType.html], documentAttributes: nil)
Make up your mind which it is.
回答2:
Using this you can Apply both things (Swift 4 Code)
@IBOutlet weak var txtWv1: UITextView!
@IBOutlet weak var txtWv2: UITextView!
class SampleViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
// Sample HTML Text
let html = "<ul> <li>Lorem ipsum dolor sit amet, consectetuer
adipiscing elit.</li> <li>Aliquam tincidunt mauris
eurisus.</li> <li>Vestibulum auctor dapibus neque.</li>
</ul>"
do {
let attrStr = try NSMutableAttributedString(data: html.data(using:
String.Encoding.unicode, allowLossyConversion: false)!,
options: [.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
// here is Your Attributed String
txtWv1.attributedText = attrStr
// Change Font or Color as per your requirement
attrStr.setFontFace(font: UIFont(name: "Roboto-Bold", size: 20)!,color: UIColor.red)
txtWv2.attributedText = attrStr
} catch let error {
print(error.localizedDescription)
}
}
}
You need to Create the Extension of NSMutableAttributedString
as Following
extension NSMutableAttributedString {
func setFontFace(font: UIFont, color: UIColor? = nil) {
beginEditing()
self.enumerateAttribute(.font, in: NSRange(location: 0, length: self.length)) { (value, range, stop) in
if let f = value as? UIFont, let newFontDescriptor = f.fontDescriptor.withFamily(font.familyName).withSymbolicTraits(f.fontDescriptor.symbolicTraits) {
let newFont = UIFont(descriptor: newFontDescriptor, size: font.pointSize)
removeAttribute(.font, range: range)
addAttribute(.font, value: newFont, range: range)
if let color = color {
removeAttribute(.foregroundColor, range: range)
addAttribute(.foregroundColor, value: color, range: range)
}
}
}
endEditing()
}
}
Output is :
来源:https://stackoverflow.com/questions/51541557/type-of-expression-is-ambiguous-without-more-context-attributed-text-in-swift-4