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
Filter by
Sorted by
Tagged with
377 votes
31 answers
432k views

How do I make an attributed string using Swift?

I am trying to make a simple Coffee Calculator. I need to display the amount of coffee in grams. The "g" symbol for grams needs to be attached to my UILabel that I am using to display the amount. The ...
dcbenji's user avatar
  • 4,648
318 votes
15 answers
248k views

How do you use NSAttributedString?

Multiple colours in an NSString or NSMutableStrings are not possible. So I've heard a little about the NSAttributedString which was introduced with the iPad SDK 3.2 (or around 3.2) and is available on ...
Brock Woolf's user avatar
  • 46.9k
300 votes
35 answers
308k views

Create tap-able "links" in the NSAttributedString of a UILabel?

Many applications have text and in this text are web hyperlinks in rounded rect. When I click them UIWebView opens. What puzzles me is that they often have custom links, for example if words starts ...
Lope's user avatar
  • 5,397
262 votes
15 answers
161k views

Bold & Non-Bold Text In A Single UILabel?

How would it be possible to include both bold and non-bold text in a uiLabel? I'd rather not use a UIWebView.. I've also read this may be possible using NSAttributedString but I have no idea how to ...
DomMaiocchi's user avatar
  • 2,747
234 votes
23 answers
287k views

How can I make a clickable link in an NSAttributedString?

It's trivial to make hyperlinks clickable in a UITextView. You just set the "detect links" checkbox on the view in IB, and it detects HTTP links and turns them into hyperlinks. However, that still ...
Duncan C's user avatar
  • 130k
188 votes
9 answers
115k views

How can I concatenate NSAttributedStrings?

I need to search some strings and set some attributes prior to merging the strings, so having NSStrings -> Concatenate them -> Make NSAttributedString is not an option, is there any way to concatenate ...
user avatar
168 votes
9 answers
213k views

Change string color with NSAttributedString?

I have a slider for a survey that display the following strings based on the value of the slider: "Very Bad, Bad, Okay, Good, Very Good". Here is the code for the slider: - (IBAction) ...
Adam's user avatar
  • 1,817
165 votes
18 answers
130k views

Convert HTML to NSAttributedString in iOS

I am using a instance of UIWebView to process some text and color it correctly, it gives the result as HTML but rather than displaying it in the UIWebView I want to display it using Core Text with a ...
Joshua's user avatar
  • 15.3k
163 votes
25 answers
137k views

boundingRectWithSize for NSAttributedString returning wrong size

I am trying to get the rect for an attributed string, but the boundingRectWithSize call is not respecting the size I pass in and is returning a rect with a single line height as opposed to a large ...
RunLoop's user avatar
  • 20.5k
157 votes
17 answers
96k views

Parsing HTML into NSAttributedText - how to set font?

I am trying to get a snippet of text that is formatted in html to display nicely on an iPhone in a UITableViewCell. So far I have this: NSError* error; NSString* source = @"<strong>Nice</...
phil's user avatar
  • 4,798
144 votes
16 answers
177k views

How to underline a UILabel in swift?

How to underline a UILabel in Swift? I searched the Objective-C ones but couldn't quite get them to work in Swift.
Esqarrouth's user avatar
  • 38.9k
134 votes
9 answers
125k views

NSAttributedString add text alignment

How can I add text alignment attribute to an NSAttributedString to center the text? Edit: Am I doing anything wrong? It doesn't seem to change the alignment. CTParagraphStyleSetting setting; setting....
aryaxt's user avatar
  • 77k
120 votes
16 answers
168k views

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

I have some HTML data, which contains headings, paragraphs , images and lists tags. Is there a way to display this data in one UITextView or UILabel?
Talha Ahmad Khan's user avatar
113 votes
17 answers
128k views

Use multiple font colors in a single label

Is there a way to use two, or even three font colors in a single label in iOS? If the text "hello, how are you" were used as an example, the "hello," would be blue, and the "how are you" would be ...
Justin Rose's user avatar
  • 1,175
113 votes
12 answers
102k views

Attributed Text Center Alignment

I have tried everything but cannot seem to center this text. Can someone please tell me where the error is. NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new; paragraphStyle....
user3255746's user avatar
  • 1,397
106 votes
16 answers
55k views

Attributed string with custom fonts in storyboard does not load correctly

We are using custom fonts in our project. It works well in Xcode 5. In Xcode 6, it works in plain text, attributed string in code. But those attributed strings set in storyboard all revert to ...
Allen Hsu's user avatar
  • 3,534
104 votes
9 answers
94k views

iphone/ipad: How exactly use NSAttributedString?

Yes, many people are saying about Rich Text in iPhone/iPad and many knows about NSAttributedString. But how to use NSAttributedString? I searched for much time, no extract clues for this. I know ...
Jack's user avatar
  • 3,943
101 votes
5 answers
66k views

Change the color of a link in an NSMutableAttributedString

I have the following code but my links are always blue. How do I cange the color of them? [_string addAttribute:NSLinkAttributeName value:tag range:NSMakeRange(position, length)]; [_string ...
cdub's user avatar
  • 25.1k
82 votes
4 answers
84k views

Example of NSAttributedString with two different font sizes?

NSAttributedString is just really impenetrable to me. I want to set a UILabel to have text of different sizes, and I gather NSAttributedString is the way to go, but I can't get anywhere with the ...
Le Mot Juiced's user avatar
79 votes
4 answers
34k views

NSAttributedString background color and rounded corners

I have a question regarding rounded corners and text background color for a custom UIView. Basically, I need to achieve an effect like this (image attached - notice the rounded corners on one side) ...
codeBearer's user avatar
  • 5,164
73 votes
24 answers
32k views

UITextView: Disable selection, allow links

I have a UITextView which displays an NSAttributedString. The textView's editable and selectable properties are both set to false. The attributedString contains a URL and I'd like to allow tapping ...
lukas's user avatar
  • 2,370
71 votes
6 answers
54k views

Adding underline to UILabel attributed string from the storyboard fails

From the storyboard I select the UILabel in question Then in Attribute Inspector > Label > [I choose] Attributed Also in Attribute Inspector > Label > Text> [I select the content] Then I click on the ...
Katedral Pillon's user avatar
70 votes
7 answers
47k views

Is it possible to get a listing of attributes and ranges for an NSMutableAttributedString?

I've created a method that takes a NSAttributedString and I'm looking to dynamically create a subview and label to put the string into. Because attributes like font and size need to be determined to ...
propstm's user avatar
  • 3,519
69 votes
10 answers
47k views

Replace substring of NSAttributedString with another NSAttributedString

I want to replace a substring (e.g. @"replace") of an NSAttributedString with another NSAttributedString. I am looking for an equivalent method to NSString's stringByReplacingOccurrencesOfString:...
Garoal's user avatar
  • 2,374
68 votes
4 answers
26k views

Center two fonts with different different sizes vertically in an NSAttributedString

I use NSAttributedString to generate a string with two different sizes. By default, its bottom alignment looks like this: But I want to center it vertically, like this: To be clear, this is a single ...
user2608857's user avatar
  • 1,071
66 votes
5 answers
76k views

how to append Attributed Text String with Attributed String in Swift

I want to append an Attributed Text with another Attributed Text in Swift. Please provide any sample code for appending operation of two attributed String in Swift.
jaydev's user avatar
  • 1,661
65 votes
10 answers
75k views

Core Text - NSAttributedString line height done right?

I'm completely in the dark with Core Text's line spacing. I'm using NSAttributedString and I specify the following attributes on it: - kCTFontAttributeName - kCTParagraphStyleAttributeName From this ...
Schoob's user avatar
  • 1,718
64 votes
3 answers
65k views

iOS NSAttributedString on UIButton

I'm using iOS 6, so attributed strings should be easy to use, right? Well... not so much. What I want to do: Using a custom subclass of UIButton (it doesn't do anything custom to titleLabel), I'd ...
mbm29414's user avatar
  • 11.6k
63 votes
1 answer
83k views

How to calculate the height of an NSAttributedString with given width in iOS 6 [duplicate]

Possible Duplicate: How to get height for NSAttributedString at a fixed width Now NSAttributedString is available in iOS 6. For layout purposes, I want to know how to calculate the required ...
Jake's user avatar
  • 1,528
59 votes
8 answers
22k views

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

Say I have an NSMutableAttributedString . The string has a varied mix of formatting throughout: Here is an example: This string is hell to change in iOS, it really sucks. However, the font per se is ...
Fattie's user avatar
  • 17.9k
52 votes
12 answers
33k views

How to get height for NSAttributedString at a fixed width

I want to do some drawing of NSAttributedStrings in fixed-width boxes, but am having trouble calculating the right height they'll take up when drawn. So far, I've tried: Calling - (NSSize) size, but ...
bonaldi's user avatar
  • 992
51 votes
3 answers
57k views

Converting a NSAttributedString into a NSString using Swift

I have a NSMutableAttributedString and want to convert it back into a simple String. var attributedString = NSMutableAttributedString(string: "hello w0rld") How can I get just the String out of a ...
ixany's user avatar
  • 5,733
50 votes
8 answers
98k views

Can I set the `attributedText` property of `UILabel`

Can I set the attributedText property of a UILabel object? I tried the below code: UILabel *label = [[UILabel alloc] init]; label.attributedText = @"asdf"; But it gives this error: Property "...
Shamsiddin Saidov's user avatar
50 votes
7 answers
81k views

How to set font size on NSAttributedString

Edit - This has been marked as duplicate, but as I state below, I am looking for a Swift solution. Everything I've found is written in Objective C. I am trying to convert HTML into an ...
Martin Muldoon's user avatar
49 votes
5 answers
72k views

How do I create a UIColor from RGBA?

I want to use NSAttributedString in my project, but when I'm trying to set color, which isn't from the standard set (redColor, blackColor, greenColor etc.) UILabel displays these letters in white ...
agoncharov's user avatar
49 votes
3 answers
48k views

centering text in a UILabel with an NSAttributedString

Going through some basic improvements to a application I am working on. Still new to the iOS swift development scene. I figured that the lines of text in my code would automatically be centered ...
cruelty's user avatar
  • 523
46 votes
3 answers
26k views

NSAttributedString ignores Autoshrink and numberOfLines for UILabel (iOS 6)

I have UILabel with number of lines = 2 system font size = 15 minimum font size = 8 Line break mode - Truncate tail When I set long text which have type NSString for UILabel it works fine and shows ...
Sergey  Pekar's user avatar
  • 8,635
46 votes
2 answers
3k views

NSRangeException Out of Bounds error

I am setting the attributed text of a label, and I am getting this strange error: Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray replaceObjectsInRange:...
Chandler De Angelis's user avatar
45 votes
4 answers
37k views

Transform an NSAttributedString to plain text

I have an instance of NSData containing attributed text (NSAttributedString) originating from an NSTextView. I want to convert the attributed string to a plain string (NSString) without any formatting ...
Roger's user avatar
  • 4,747
44 votes
11 answers
36k views

Make link in UILabel.attributedText *not* blue and *not* underlined

I want some words within my OHAttributedLabel to be links, but I want them to be colors other than blue and I don't want the underline. This is giving me a blue link with underlined text: -(void)...
OdieO's user avatar
  • 6,926
42 votes
9 answers
38k views

replace entire text string in NSAttributedString without modifying other attributes

I have a reference to NSAttributedString and i want to change the text of the attributed string. I guess i have to created a new NSAttributedString and update the reference with this new string. ...
Chirag Jain's user avatar
  • 2,388
40 votes
5 answers
23k views

How can I both stroke and fill with NSAttributedString w/ UILabel

Is it possible to apply both stroke and fill with an NSAttributedString and a UILabel?
Piotr Tomasik's user avatar
40 votes
4 answers
19k views

NSAttributedString superscript styling

I want to superscript all the instances of ® character in a block of text (legal disclaimer, naturally ;)) and the default way NSAttributedString is not very good. If I just let the character be and ...
SaltyNuts's user avatar
  • 5,118
39 votes
7 answers
32k views

Type 'NSAttributedStringKey' (aka 'NSString') has no member 'font'

Just updated pods for xcode 9 and I'm getting the error below for Cosmos. Type 'NSAttributedStringKey' (aka 'NSString') has no member 'font'
Chaudhry Talha's user avatar
38 votes
9 answers
81k views

Tap on a part of text of UILabel

I have a problem that boundingRectForGlyphRange always returns CGRect.zero "0.0, 0.0, 0.0, 0.0". For example, I am coding for touching on a part of text of UILabel feature. My text has first ...
Ashley's user avatar
  • 509
37 votes
3 answers
12k views

Wrapping Text in a UITextView Around a UIImage WITHOUT CoreText

Is there a way to wrap text from a UITextView around a UIImage without using CoreText? I have been playing around with attributed strings without much luck, and CoreText just seems extremely ...
harryisaac's user avatar
  • 1,141
36 votes
6 answers
28k views

how to resize an image or done as a NSAttributedString NSTextAttachment (or set its initital size)

I have a NSAttributedString to which I am adding a NSTextAttachment. The image is 50w by 50h but I'd like it to scale down to reflect the line height of the attributed string. I thought this would be ...
timpone's user avatar
  • 19.6k
35 votes
8 answers
14k views

How to set color of templated image in NSTextAttachment

How can I set the color of a templated image that is an attachment on an attributed string? Background: I've got a UILabel and I'm setting its attributedText to an NSAttributedString. The ...
Ghazgkull's user avatar
  • 970
34 votes
7 answers
28k views

NSAttributedString inserting a bullet point?

So I have an NSAttributedString I want to insert a bullet point at the beginning of a portion of text. How can I do this? How do I create a CTPAragraphStyle that creates this bullet point when I ...
aryaxt's user avatar
  • 77k
34 votes
1 answer
48k views

How can I set the color and alignment of attributed text in a UITextView in iOS 7?

The formatting of my textViews worked fine in iOS 6, but no longer in iOS 7. I understand with Text Kit much of the under the hood stuff has changed. It's become really quite confusing, and I'm hoping ...
Joe's user avatar
  • 864

1
2 3 4 5
46