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
13
votes
3 answers

CGRect for selected UITextRange adjustment for multiline text?

I've used this answer in order to create a CGRect for a certain range of text. In this UITextView I've set it's attributedText (so I've got a bunch of styled text with varying glyph sizes). This works great for the first line of text that's left…
brandonscript
  • 68,675
  • 32
  • 163
  • 220
13
votes
4 answers

How to get a selected range of text from a UITextView or UITextField

I am trying to get the selected range of text from a UITextView (and or UITextField) so that I can edit the selected text, or modify an attributed string. The method below is triggered when I make a selection, but the code in the method returns null…
OWolf
  • 5,012
  • 15
  • 58
  • 93
13
votes
2 answers

How to render a multiline UILabel with NSMutableAttributedString

I am trying to create a multiline UILabel with an NSMutableAttributedString. This works fine when I assign an attribute to the complete string like this: UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,100)]; [contentLabel…
adriaan
  • 1,574
  • 14
  • 33
12
votes
2 answers

NSBackgroundColorAttributeName-like attribute in NSAttributedString on iOS?

I was planning on using NSAttributedString to highlight portions of strings with the matching query of a user's search. However, I can't find an iOS equivalent of NSBackgroundColorAttributeName—there's no kCTBackgroundColorAttributeName. Does such a…
kevboh
  • 5,207
  • 5
  • 38
  • 54
12
votes
3 answers

Convert Markdown-formatted string to NSAttributedString in Swift

Is there a way to convert a plain text string containing Markdown text (i.e., # heading, * list item, [a link](http://example.com), etc.) to an NSAttributedString in Swift? I suppose I could perform some kind of regex search for the indices of…
Matt
  • 2,576
  • 6
  • 34
  • 52
12
votes
2 answers

How to use SF Fonts with attributed text in Interface Builder

I'm on Xcode 9.x and I want to use attributed text on a UILabel (in InterfaceBuilder). I want to set : System font (San Fracisco) and some words in bold style. But it doesn't work (italic works but not bold ...). I can't choose System font (San…
QLag
  • 823
  • 1
  • 13
  • 33
12
votes
7 answers

Swift 4 Label attributes

I'm moving from swift 3 to swift 4. I have UILabels that I am giving very specific text properties to the label. I'm getting an 'unexpectedly found nil while unwrapping optional value' error when strokeTextAttributes is being initialized. I'm…
Blue
  • 1,408
  • 4
  • 17
  • 36
12
votes
3 answers

NSAttributedString: Fit image to container

I have a NSAttributedString which is made from HTML and it displays some images. The problem is that the images are bigger than the container and I wonder how to fit them in it. Thanks for your help
Florentin
  • 1,433
  • 2
  • 13
  • 22
12
votes
2 answers

NSAttributedString initWithData and NSHTMLTextDocumentType crash if not on main thread

calling NSAttributedString * as = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:…
Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179
12
votes
4 answers

How to bold some words in my UITextView using NSMutableAttributedString?

I have a UITextView and there are certain words I'm casting with NSString stringWithFormat that I'd like to be bolded. I have looked around Stack Overflow and tried to follow the the postings but I guess I'm not understanding it. Here's what I've…
12
votes
1 answer

How to apply both bold and italic font to an NSAttributedString?

I'm trying to parse my own HTML string into an NSAttributedString for rendering in a UITextView. So, for when the string appears as such: This should be both bold and italic I want to apply a distinct bold and italic font…
NYC Tech Engineer
  • 1,845
  • 2
  • 22
  • 23
12
votes
1 answer

Apply Custom font to Attributed string Which Converts from HTML String

I used UITextView in which i display NSAttributedString. i got HTML string from server . I converted HTML to NSAttributedString using below code. NSMutableAttributedString *attrib = [[NSMutableAttributedString alloc]initWithData:[strHTML…
ChintaN -Maddy- Ramani
  • 5,156
  • 1
  • 27
  • 48
12
votes
6 answers

UILabel: Custom underline color?

I need the text of a UILabel to be black, but have a light gray underline under the text. Is this possible with NSAttributedString or TTTAttributedLabel? Or is custom drawing using Core Graphics needed? CLARIFICATION: I need a specific color text on…
ninjaneer
  • 6,951
  • 8
  • 60
  • 104
12
votes
2 answers

How to stop a time UILabel from resizing at every time increment?

I have a stopwatch feature in my app that uses a centered attributed UILabel with a proportionally spaced font to render time. At every time increment the width of the label changes, creating a bouncing effect that looks especially bad at fast…
12
votes
0 answers

iOS NSAttributedString emoji transparency/alpha

I'm using an UITextView and want some parts of the text to be transparent. I'm currently using an NSAttributedString with NSForegroundColorAttribute having the color with the desired transparency. It works for regular characters but not for…
ZeCodea
  • 1,071
  • 9
  • 26