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.

How to make subscripts and superscripts using NSAttributedString?

Here’s what I did in iOS 6. First add the CoreText, and QuartzCore frameworks. Then import: #import <QuartzCore/QuartzCore.h> #import <CoreText/CTStringAttributes.h> #import <CoreText/CoreText.h> I made a small function that inputs a plain NSString and exports a NSMutableAttributedString with the last character in superscript. This can be modified to allow setting superscript or subscript, change kCTSuperscriptAttributeName value … Read more

NSAttributedString, change the font overall BUT keep all other attributes?

Since rmaddy’s answer did not work for me (f.fontDescriptor.withFace(font.fontName) does not keep traits like bold), here is an updated Swift 4 version that also includes color updating: 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 … Read more

How do i convert NSAttributedString into HTML string?

Use dataFromRange:documentAttributes: with the document type attribute (NSDocumentTypeDocumentAttribute) set to HTML (NSHTMLTextDocumentType): NSAttributedString *s = …; NSDictionary *documentAttributes = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}; NSData *htmlData = [s dataFromRange:NSMakeRange(0, s.length) documentAttributes:documentAttributes error:NULL]; NSString *htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];

Tap on a part of text of UILabel

#swift 4.2 Please find the solution here for getting specific text action of Label. Label declaration @IBOutlet weak var lblTerms: UILabel! Set attributed text to the label let text = “Please agree for Terms & Conditions.” lblTerms.text = text self.lblTerms.textColor = UIColor.white let underlineAttriString = NSMutableAttributedString(string: text) let range1 = (text as NSString).range(of: “Terms & … Read more

Superscript cents in an attributed string

You have to use NSBaselineOffsetAttributedName. From the doc: NSBaselineOffsetAttributeName The value of this attribute is an NSNumber object containing a floating point value indicating the character’s offset from the baseline, in points. The default value is 0. Available in iOS 7.0 and later. From your example: [buyString addAttribute:NSBaselineOffsetAttributeName value:@(10.0) range:NSMakeRange(2, buyString.length – 2)]; You may … Read more