iOS crash report “unexpected start state” exception?

The above crash happens when you try initializing a NSAttibutedString with html content from any other thread than the main thread. So the solution here is to make sure the above NSAttributedString initialization is always called from the main thread. DispatchQueue.main.async { let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [.documentType: NSAttributedString.DocumentType.html] let htmlString = try? NSAttributedString(data: htmlData, … Read more

Is it possible to get a listing of attributes and ranges for an NSMutableAttributedString?

Swift 5 – 4 let attributedText = NSAttributedString(string: “Hello, playground”, attributes: [ .foregroundColor: UIColor.red, .backgroundColor: UIColor.green, .ligature: 1, .strikethroughStyle: 1 ]) // retrieve attributes let attributes = attributedText.attributes(at: 0, effectiveRange: nil) // iterate each attribute for attr in attributes { print(attr.key, attr.value) } In case, that you have defined attributedText of label. Swift 3 var … Read more

Underline part of a string using NSMutableAttributedString in iOS 8 is not working

Update: By investigating this question: Displaying NSMutableAttributedString on iOS 8 I finally found the solution! You should add NSUnderlineStyleNone at the beginning of the string. Swift 4.2 (none was removed): let attributedString = NSMutableAttributedString() attributedString.append(NSAttributedString(string: “test “, attributes: [.underlineStyle: 0])) attributedString.append(NSAttributedString(string: “s”, attributes: [.underlineStyle: NSUnderlineStyle.single.rawValue])) attributedString.append(NSAttributedString(string: “tring”, attributes: [.underlineStyle: 0])) Objective-C: NSMutableAttributedString *attributedString = [[NSMutableAttributedString … Read more

Change the color of a link in an NSMutableAttributedString

Swift Updated for Swift 4.2 Use linkTextAttributes with a UITextView textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.green] And in context: let attributedString = NSMutableAttributedString(string: “The site is www.google.com.”) let linkRange = (attributedString.string as NSString).range(of: “www.google.com”) attributedString.addAttribute(NSAttributedString.Key.link, value: “https://www.google.com”, range: linkRange) let linkAttributes: [NSAttributedString.Key : Any] = [ NSAttributedString.Key.foregroundColor: UIColor.green, NSAttributedString.Key.underlineColor: UIColor.lightGray, NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue ] // textView is a … Read more

Making text bold using attributed string in swift

Usage: let label = UILabel() label.attributedText = NSMutableAttributedString() .bold(“Address: “) .normal(” Kathmandu, Nepal\n\n”) .orangeHighlight(” Email: “) .blackHighlight(” prajeet.shrestha@gmail.com “) .bold(“\n\nCopyright: “) .underlined(” All rights reserved. 2020.”) Result: Here is a neat way to make a combination of bold and normal texts in a single label plus some other bonus methods. Extension: Swift 5.* extension NSMutableAttributedString … Read more

Changing specific text’s color using NSMutableAttributedString in Swift

let mainString = “Hello World” let stringToColor = “World” SWIFT 5 let range = (mainString as NSString).range(of: stringToColor) let mutableAttributedString = NSMutableAttributedString.init(string: mainString) mutableAttributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: range) textField = UITextField.init(frame: CGRect(x:10, y:20, width:100, height: 100)) textField.attributedText = mutableAttributedString SWIFT 4.2 let range = (mainString as NSString).range(of: stringToColor) let mutableAttributedString = NSMutableAttributedString.init(string: mainString) mutableAttributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)