Use multiple font colors in a single label

Reference from here. First of all initialize of you NSString and NSMutableAttributedString as below. var myString:NSString = “I AM KIRIT MODI” var myMutableString = NSMutableAttributedString() In ViewDidLoad override func viewDidLoad() { myMutableString = NSMutableAttributedString(string: myString, attributes: [NSFontAttributeName:UIFont(name: “Georgia”, size: 18.0)!]) myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:2,length:4)) // set label Attribute labName.attributedText = myMutableString super.viewDidLoad() } OUTPUT MULTIPLE … Read more

Swift: Display HTML data in a label or textView [duplicate]

For Swift 5: extension String { var htmlToAttributedString: NSAttributedString? { guard let data = data(using: .utf8) else { return nil } do { return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil) } catch { return nil } } var htmlToString: String { return htmlToAttributedString?.string ?? “” } } Then, whenever you want to … Read more

NSAttributedString add text alignment

NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new; paragraphStyle.alignment = NSTextAlignmentCenter; NSAttributedString *attributedString = [NSAttributedString.alloc initWithString:@”someText” attributes: @{NSParagraphStyleAttributeName:paragraphStyle}]; Swift 4.2 let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = NSTextAlignment.center let attributedString = NSAttributedString(string: “someText”, attributes: [NSAttributedString.Key.paragraphStyle : paragraphStyle])

How to underline a UILabel in swift?

You can do this using NSAttributedString Example: let underlineAttribute = [NSAttributedString.Key.underlineStyle: NSUnderlineStyle.thick.rawValue] let underlineAttributedString = NSAttributedString(string: “StringWithUnderLine”, attributes: underlineAttribute) myLabel.attributedText = underlineAttributedString EDIT To have the same attributes for all texts of one UILabel, I suggest you to subclass UILabel and overriding text, like that: Swift 5 Same as Swift 4.2 but: You should prefer … Read more

Parsing HTML into NSAttributedText – how to set font?

Swift 2 version, based on the answer given by Javier Querol extension UILabel { func setHTMLFromString(text: String) { let modifiedFont = NSString(format:”<span style=\”font-family: \(self.font!.fontName); font-size: \(self.font!.pointSize)\”>%@</span>”, text) as String let attrStr = try! NSAttributedString( data: modifiedFont.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding], documentAttributes: nil) self.attributedText = attrStr } } Swift 3.0 and iOS 9+ … Read more

boundingRectWithSize for NSAttributedString returning wrong size

Looks like you weren’t providing the correct options. For wrapping labels, provide at least: CGRect paragraphRect = [attributedText boundingRectWithSize:CGSizeMake(300.f, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) context:nil]; Note: if the original text width is under 300.f there won’t be line wrapping, so make sure the bound size is correct, otherwise you will still get wrong results.

Convert HTML to NSAttributedString in iOS

In iOS 7, UIKit added an initWithData:options:documentAttributes:error: method which can initialize an NSAttributedString using HTML, eg: [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil]; In Swift: let htmlData = NSString(string: details).data(using: String.Encoding.unicode.rawValue) let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html] let attributedString = try? NSMutableAttributedString(data: htmlData ?? Data(), options: options, documentAttributes: nil)

How can I concatenate NSAttributedStrings?

I’d recommend you use a single mutable attributed string a @Linuxios suggested, and here’s another example of that: NSMutableAttributedString *mutableAttString = [[NSMutableAttributedString alloc] init]; NSString *plainString = // … NSDictionary *attributes = // … a dictionary with your attributes. NSAttributedString *newAttString = [[NSAttributedString alloc] initWithString:plainString attributes:attributes]; [mutableAttString appendAttributedString:newAttString]; However, just for the sake of getting … Read more

How can I make a clickable link in an NSAttributedString?

Use NSMutableAttributedString. NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:@”Google”]; [str addAttribute: NSLinkAttributeName value: @”http://www.google.com” range: NSMakeRange(0, str.length)]; yourTextView.attributedText = str; Edit: This is not directly about the question but just to clarify, UITextField and UILabel does not support opening URLs. If you want to use UILabel with links you can check TTTAttributedLabel. Also you should … Read more

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