nsattributedstring

swift: The HTML data converted to NSAttributed string doesn't make the links clickable in label

时光毁灭记忆、已成空白 提交于 2019-12-13 03:07:39
问题 I have a text in HTML format. I am using the property of NSAttributed string to parse it. It pareses the text nicely and displays on the label. However, the parsing of the anchor tag doesn't make the link clickable. This is the following code that I am using for parsing. extension String { var htmlToAttributedString: NSAttributedString? { guard let data = data(using: .utf8) else { return NSAttributedString() } do { return try NSAttributedString(data: data, options: [.documentType:

convert a String to NSAttributedString in a specific manner

不羁的心 提交于 2019-12-13 02:18:38
问题 so i have a String which looks like this : Swift , VisualBasic , Ruby i wanna convert this string to something like this : basically i wanna create a background behind a single word , yeah i can use the NSTokenField libraries for getting this behaviour but my text is not manually entered by user its pre structured (from an array) and i dont want the whole behaviour of NSTokeField i just want the appearance like this and selection (by selection i mean to clear a word at one single tap on

Crashing on range on NSTextStorage

只愿长相守 提交于 2019-12-12 16:04:14
问题 I have a UITextView that should receive NSAttributesString contracted from HTML. The end result is black text with custom underline for links. I am crashing on range. Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds' Here is the code: import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let text = "random text <a href='http://www.google.com'>http://www.google

Change just Font of AttributedText in Swift

两盒软妹~` 提交于 2019-12-12 15:18:20
问题 I have a number of UILabels created in IB which all have attributed text. Each label's text contains multiple lines of different font sizes and colors. At run-time, I want to be able to change just the font name of these labels without changing the existing font sizes or colors. I have researched and could not find a straight forward method to achieve this. Any ideas? 回答1: You first need to understand the lingo Apple uses to describe a typeface: Helvetica is a family Helvetica Bold ,

Split Attributed String and Retain Formatting

有些话、适合烂在心里 提交于 2019-12-12 09:53:54
问题 How can you take an existing NSAttributedString and divide it based on a predefined separator while maintaining formatting? It does not seem that componentsSeparatedByString will operate on an NSAttributedString. My current workaround produces the splits at the correct points, but only outputs an NSString. Thus losing formatting. NSData *rtfFileData = [NSData dataWithContentsOfFile:path]; NSAttributedString *rtfFileAttributedString = [[NSAttributedString alloc] initWithData:rtfFileData

UITextView NSAttributedString Size

泪湿孤枕 提交于 2019-12-12 07:49:01
问题 I'm working on an app which uses a UITextView. The UITextView should grow or shrink to fit its text, both vertically and horizontally. To do this, I'm overriding sizeToFit in a subclass, and I set the bounds like so: - (void)sizeToFit { [self setBounds:(CGRect){.size = self.attributedText.size}]; } The problem is that this size just doesn't reflect the correct size of the string, as the UITextView clips the text. I'm setting the edge insets to zero, so that shouldn't be an issue right? At

Copy Text with Formatting - iOS 6 NSAttributedString to Pasteboard

拟墨画扇 提交于 2019-12-12 07:16:34
问题 How can I programmatically copy formatted text (e.g. italic ) from a UITextView, so that when pasted into another program (e.g. Mail) the formatting will be retained? I'm assuming the correct approach is to copy an NSAttributedString to the pasteboard. Right now, I am able to copy a regular string to the pasteboard via the following: NSString *noteTitleAndBody = [NSString stringWithFormat:@"%@\n%@", [document title], [document body]]; UIPasteboard *pasteboard = [UIPasteboard generalPasteboard

iOS getting date string from RSS Feed

大兔子大兔子 提交于 2019-12-12 04:44:39
问题 I have an XML parser that uses RaptureXML to grab each item and display it within a table cell. I already can get the title and description, but I can't seem to work out how to get the date. Here's what I have so far for the date: NSString* dateString; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"EEEE MMMM d, YYYY"]; dateString = [formatter stringFromDate:self.pubDate]; [articleAbstract appendAttributedString: [[NSAttributedString alloc]

In Swift, how can I change an attribute of part of a string?

雨燕双飞 提交于 2019-12-12 04:41:51
问题 I am trying to display a score with some text. The score is displayed in the middle of a sentence, and I want the font to be bigger for the score than the rest of the text. My code is as follows: let fontSizeAttribute = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 43)] let myString = String(describing: Int(finalScore!.rounded(toPlaces: 0))) let attributedString = NSAttributedString(string: myString, attributes: fontSizeAttribute) scoreLabel.text = "Your score is \(attributedString)%

Can I make a button in swift whose text has multiple colors?

北慕城南 提交于 2019-12-12 03:50:22
问题 Can I make a button in swift whose text has multiple colors? The button text will change dynamically, so I cannot make an image of it. I have tried to make two attributed strings with different colors, concatenate them, and set the button's text to that. Unfortunately, that does not preserve the different colors and just adds plaintext describing the nsattribtues at the end of the string. let currDescString = NSAttributedString(string: descriptor) let editString = NSAttributedString(string: "