Replace substring of NSAttributedString with another NSAttributedString

Convert your attributed string into an instance of NSMutableAttributedString. The mutable attributed string has a mutableString property. According to the documentation: “The receiver tracks changes to this string and keeps its attribute mappings up to date.” So you can use the resulting mutable string to execute the replacement with replaceOccurrencesOfString:withString:options:range:.

UITextView: Disable selection, allow links

I find the concept of fiddling with the internal gesture recognizers a little scary, so tried to find another solution. I’ve discovered that we can override point(inside:with:) to effectively allow a “tap-through” when the user isn’t touching down on text with a link inside it: // Inside a UITextView subclass: override func point(inside point: CGPoint, … Read more

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

Swift 5 – 4 let attributedText = NSAttributedString(string: “Hello, playground”, attributes: [ .foregroundColor: UIColor.red, .backgroundColor: UIColor.green, .ligature: 1, .strikethroughStyle: 1 ]) // retrieve attributes let attributes = attributedText.attributes(at: 0, effectiveRange: nil) // iterate each attribute for attr in attributes { print(attr.key, attr.value) } In case, that you have defined attributedText of label. Swift 3 var … Read more

Adding underline to UILabel attributed string from the storyboard fails

Here is the solution in storyboard: Open the Attribute Inspector (make sure label is selected), Change the dropdown value from ‘Plain’ to ‘Attributed’. Now a a small text editor will be visible under the font of the label. Select the text of the label, right click and change the font to ‘underline’. I also have … Read more

NSAttributedString background color and rounded corners

TL;DR; Create a custom-view, which renders same old NSAttributedString, but with rounded-corners. Unlike Android’s SpannableString, iOS does not support “custom-render for custom-string-attributes”, at least not without an entire custom-view (at time of writing, 2022). I managed to achieve the above effect, so thought I’d post an answer for the same. If anyone has any suggestions … Read more

Example of NSAttributedString with two different font sizes?

You would do something like this… NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@”Presenting the great… Hulk Hogan!”]; [hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20.0] range:NSMakeRange(24, 11)]; This will set the last two words in 20-point text; the rest of the string will use the default value (which I believe is 12 points). The thing that might be confusing about … Read more

Change the color of a link in an NSMutableAttributedString

Swift Updated for Swift 4.2 Use linkTextAttributes with a UITextView textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.green] And in context: let attributedString = NSMutableAttributedString(string: “The site is www.google.com.”) let linkRange = (attributedString.string as NSString).range(of: “www.google.com”) attributedString.addAttribute(NSAttributedString.Key.link, value: “https://www.google.com”, range: linkRange) let linkAttributes: [NSAttributedString.Key : Any] = [ NSAttributedString.Key.foregroundColor: UIColor.green, NSAttributedString.Key.underlineColor: UIColor.lightGray, NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue ] // textView is a … Read more

iphone/ipad: How exactly use NSAttributedString?

Starting from the iOS 6.0 you can do it like that: NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@”Hello. That is a test attributed string.”]; [str addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(3,5)]; [str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(10,7)]; [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@”HelveticaNeue-Bold” size:20.0] range:NSMakeRange(20, 10)]; label.attributedText = str;

Attributed Text Center Alignment

In Swift 5 let paragraph = NSMutableParagraphStyle() paragraph.alignment = .center textView.attributedText = NSAttributedString(string: “String”, attributes: [.paragraphStyle: paragraph]) In Swift-4 let paragraph = NSMutableParagraphStyle() paragraph.alignment = .center let attributes: [NSAttributedString.Key : Any] = [NSAttributedString.Key.paragraphStyle: paragraph] let attrString = NSAttributedString(string:”string”, attributes: attributes) textView.attributedText = attrString In Swift-3 let paragraph = NSMutableParagraphStyle() paragraph.alignment = .center let attributes: [String … Read more