core-text
NSAttributedString superscript styling
The following code seems to do the trick: UIFont *fnt = [UIFont fontWithName:@”Helvetica” size:20.0]; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@”GGG®GGG” attributes:@{NSFontAttributeName: [fnt fontWithSize:20]}]; [attributedString setAttributes:@{NSFontAttributeName : [fnt fontWithSize:10] , NSBaselineOffsetAttributeName : @10} range:NSMakeRange(3, 1)];
Core Text calculate letter frame in iOS
You did an impressive amount of work in your question and were so close on your own. The problem you were having comes from this line of code where you position the bounding boxes for each frame: _characterFrames[ic].origin = CGPointMake(startOffset, lineOrigin.y); The problem with it is that you are overriding whatever offset the frame already … Read more
Core Text – NSAttributedString line height done right?
Objective-C NSInteger strLength = [myString length]; NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; [style setLineSpacing:24]; [attString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, strLength)]; Swift 5 let strLength = myString.length() var style = NSMutableParagraphStyle() style.lineSpacing = 24 attString.addAttribute(.paragraphStyle, value: style, range: NSRange(location: 0, length: strLength))
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])
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)