Questions tagged [nsattributedstring]

NSAttributedString objects manage character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string.

NSAttributedString and NSMutableAttributedString declare the programmatic interface for read-only attributed strings and modifiable attributed strings, respectively. The Foundation framework defines only the basic functionality for attributed strings; in Mac OS X, additional methods supporting RTF, graphics attributes, and drawing attributed strings are described in NSAttributedString Additions, found in the Application Kit. The Application Kit also uses a subclass of NSMutableAttributedString, called NSTextStorage, to provide the storage for the Application Kit’s extended text-handling system.

In Mac OS X, the Application Kit also uses NSParagraphStyle and its subclass NSMutableParagraphStyle to encapsulate the paragraph or ruler attributes used by the NSAttributedString classes.

An attributed string identifies attributes by name, storing a value under the name in an NSDictionary object. In Mac OS X, standard attribute keys are described in the “Constants” section of NSAttributedString Application Kit Additions Reference. You can also assign any attribute name/value pair you wish to a range of characters—it is up to your application to interpret custom attributes (see Attributed String Programming Guide). If you are using attributed strings with the Core Text framework, you can also use the attribute keys defined by that framework.

Note that the default font for NSAttributedString objects is Helvetica 12-point, which differs from the Mac OS X system font Lucida Grande, so you may wish to create the string with non-default attributes suitable for your application using, for example, initWithString:attributes:.

Resource

2249 questions
11
votes
5 answers

NSAttributedString initialization throws NSRangeException

I wrote a simple extension to decode the html entities: extension String { func htmlDecode() -> String { if let encodedData = self.data(using: String.Encoding.unicode) { let attributedString = try! NSAttributedString(data:…
Fabio Poloni
  • 8,219
  • 5
  • 44
  • 74
11
votes
2 answers

RTF file to attributed string

I'm trying to read RTF file contents to attributed string, but attributedText is nil. Why? if let fileURL = NSBundle.mainBundle().URLForResource(filename, withExtension: "rtf") { var error: NSError? if let attributedText =…
Shmidt
  • 16,436
  • 18
  • 88
  • 136
11
votes
1 answer

Cannot find System Bold Font for Attribute String iOS Xcode

I my App. at many places I have used System Bold Font, which has became standard in my App. If I use Attributed String by default font which appears is "Helvetica Neue". In all Fonts, I searched for System Bold Font, but could not find it. So, how…
NNikN
  • 3,720
  • 6
  • 44
  • 86
11
votes
1 answer

Bulletpoint spacing and linebreak start position with NSHTMLTextDocumentType

WIth iOS 7 came NSHTMLTextDocumentType, which Im using the code below to parse html and show it in a UITextView. It works perfectly, except with bullet points. How can I both change the spacing on each side of the bulletpoints(the space between the…
AlexanderN
  • 2,610
  • 1
  • 26
  • 42
11
votes
2 answers

How to allow NSAttributedString text be typed into a UITextView?

I'm trying to allow different styles of text to be typed into a UITextView, a bit like a text editor using simple attributes such as bold or italics. I understand by using the textView's attributedText property I can apply attributes to certain…
Joe
  • 854
  • 3
  • 11
  • 21
11
votes
1 answer

iOS - Auto-shrink UILabel with Attributed text

I have UILabel which contains two attributed strings separated by a new line. FIrst string has font size set to 17 and the second one to 14. I want my first NSMutableAttributedString be resized to minimum font size if its content can't fit in a…
Oleg
  • 2,984
  • 8
  • 43
  • 71
11
votes
4 answers

Best approach for UITableViewCell with complex format

I'm new to iOS development pondering how best to approach a fairly simple design problem. I want to display a set of items, each one of which has the structure as sketched. In a given set, not more than 10's of items. Each item includes a thumbnail…
Phil Mitchell
  • 699
  • 8
  • 25
11
votes
2 answers

NSMutableAttributedStrings - objectAtIndex:effectiveRange:: Out of bounds

I'm trying to add some fancy text to a label, but I've run into some problems with the NSMutableAttributedString class. I was trying to achieve four this: 1. Change font, 2. Underline range, 3. Change range color, 4. Superscript range. This code: -…
425nesp
  • 6,936
  • 9
  • 50
  • 61
10
votes
1 answer

Value of type 'NSAttributedString' has no member 'addAttributes'

I checked the documentation and the method is there. And when I look in the .h file I do see the function. I've tried cleaning and rebuilding and I still get this error after migrating to Swift 4. Value of type 'NSAttributedString' has no member…
Aggressor
  • 13,323
  • 24
  • 103
  • 182
10
votes
3 answers

NSAttributedString not rendering on time - Swift

So, I'm having issues in a project where certain strings are not being rendered properly in some old iPhones with certain iOS versions (specifically it does not work for iPhone 5 with iOS10, while it does for iOS9.3, oddly enough). To reduce the…
pyriku
  • 1,251
  • 7
  • 17
10
votes
1 answer

Assigning NSAttributedString to UILabel is slow?

I have been trying to troubleshoot glitchy table view scrolling performance. Running it through Instrument's Time Profiler shows that decent amount of time is spent setting NSAttributedString to UILabel. Is this a known problem? If so, is there a…
dfujiwara
  • 353
  • 2
  • 11
10
votes
7 answers

NSAttributedString click event in UILabel using Swift

Suppose I have an AttributedString: "Already have an account? Sign in!". I am placing this String in UILabel. Now when a user clicks on "Sign in!", the current viewController should go to another viewController or some function should be called…
Rishu Agrawal
  • 157
  • 1
  • 1
  • 10
10
votes
4 answers

iPhone CoreText: Find the pixel coordinates of a substring

Here's a screenshot of the twitter app for reference: http://screencast.com/t/YmFmYmI4M What I want to do is place a floating pop-over on top of a substring in an NSAttributedString that could span multiple lines. NSAttributedString is a…
jtrim
  • 3,465
  • 4
  • 31
  • 44
10
votes
3 answers

How can I clear the contents of an NSMutableAttributedString?

I have an ivar which is alloc-inited in the init of an object: attString = [[NSMutableAttributedString alloc] init]; On a loop, I want to clear the contents of attString and re-use it. How do I do this? Thanks!
jowie
  • 8,028
  • 8
  • 55
  • 94
10
votes
4 answers

Can I set default font when translating HTML into NSAttributedString?

I'm trying to show HTML in a UILabel, like NSString * htmlString = @"Some html string \n This is some text!"; NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString…
Rick
  • 235
  • 4
  • 13