nsattributedstring

NSAttributedString boundingRect returns wrong height

末鹿安然 提交于 2019-12-11 06:19:54
问题 I calculate the height of an NSAttributedString like this. let maxSize = NSSize(width: w, height: CGFloat.greatestFiniteMagnitude) let rect = boundingRect(with: maxSize, options: [.usesFontLeading, .usesLineFragmentOrigin]) let height = rect.integral.size.height I tried about every "hack" that was mentioned on SO, yet the string height gets more inaccurate the smaller the width gets (the calculated height is larger than the actual height). According to other posts the following things cause

Images being flipped when adding to NSAttributedString

微笑、不失礼 提交于 2019-12-11 05:58:27
问题 I have a strange problem when resizing an image that's in a NSAttributedString. The resizing extension is working fine, but when the image is added to the NSAttributedString, it gets flipped vertically for some reason. This is the resizing extension: extension NSImage { func resize(containerWidth: CGFloat) -> NSImage { var scale : CGFloat = 1.0 let currentWidth = self.size.width let currentHeight = self.size.height if currentWidth > containerWidth { scale = (containerWidth * 0.9) /

Change only fontsize of NSAttributedString

时光毁灭记忆、已成空白 提交于 2019-12-11 05:56:48
问题 I have a NSAttributedString that was loaded from a RTF file, so it already holds several font-attributes for different ranges. Now I want to adapt the font size to the screensize of the device, but when I add a whole new font attribute with a new size, the other fonts disappear. Is there a way to change only the font size for the whole string? 回答1: If you only want to change the size of any given font found in the attributed string then you can do: let newStr = someAttributedString

nsattributedstring tappable iOS

旧城冷巷雨未停 提交于 2019-12-11 04:36:33
问题 I figured out how to make in core text a url tappable but i can not figure out how to make other words tappable like 'my name is @george' . I would like @george to be tappable in core text. Is there a way? I am trying like this to make it tappable: CFAttributedStringSetAttribute(string, CFRangeMake( 0, mystring.length ), kCTFontAttributeName, ctFontBold ); CFAttributedStringSetAttribute(string, CFRangeMake( mystring.length, linestring.length ), kCTFontAttributeName, ctFont);

Detecting taps on link in NSAttributedString

纵然是瞬间 提交于 2019-12-11 04:04:18
问题 I'm dynamically building views and printing attributed text on them using drawInRect:. Some of the text may include web links. How can I detect taps on web links so I can load the link in a UIWebView? I'm not using a UILabel, UITextView or UITextField to display the text because the text includes a mixture of text and images. This is some of the code in drawRect for my UIView subclass that draws the text. //draw the text NSMutableAttributedString *attString = [[NSMutableAttributedString alloc

NSAttributedString does not appear when generating PDF

别等时光非礼了梦想. 提交于 2019-12-11 03:53:23
问题 This used to work and now, all of a sudden, it stopped working: For an iOS 7 iPad App, I generate a PDF with UIGraphicsBeginPDFContextToFile(pdfPathWithFileName, CGRectZero, nil); ... UIGraphicsEndPDFContext(); Within that code block, the following methods used to render text underlined, but recently, it just stopped working and any text passed to the method is not rendered at all: +(void)drawUnderlinedText:(NSString *)text withFont:(UIFont *)font andColor:(UIColor *)color andLocationX:(int

NSTextAlignmentCenter and NSTextAlignmentRight are the wrong way round in NSTextTab?

白昼怎懂夜的黑 提交于 2019-12-11 03:38:09
问题 Can anyone please check something for me... Just to make sure I'm not going mad! I created an NSMutableParagraphStyle with tabStops , but they weren't appearing where I was expecting: float width = self.myLabel.frame.size.width; self.tabStyle.tabStops = @[[[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentRight location:width - 50 options:nil], [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentRight location:width options:nil]]; Then I created a convenience method to create an

iOS 7 using an HTML string as an NSAttributedString AND setting the font?

亡梦爱人 提交于 2019-12-11 03:28:11
问题 I have a UITextView in which I'm trying to display some attributed text which comes in the form of HTML. It uses things like inline bolding and italicizing. The client now wants the font on this to change. However, whenever I change the font, it seems to disregard any of the HTML formatting. Is there any way to change the font while retaining the other formatting attributes, like bolding an italicizing? 回答1: The way I was able to get it to work was to prepend my string with <span style=\"font

iOS - Highlight One Word Or Multiple Words In A UITextView

时光毁灭记忆、已成空白 提交于 2019-12-11 02:42:46
问题 In my project, I'm making a small piece of functionality that allows a user to read a body of text (in a UITextView ) faster by having one word being highlighted at a time, or even multiple if a user wants. One by one, each word in the UITextView should be highlighted at the speed the user sets. Is there any way I could highlight the background of a word in a UITextView ? I think it has something to do with attributed strings, but I can't find anything that highlights the background of an

Convert attributed text to HTML

空扰寡人 提交于 2019-12-11 02:03:43
问题 In my case I have to convert NSHTMLTextDocumentType document to NSMutableAttributedString for which I use below snippet [[NSMutableAttributedString alloc] initWithData:[text dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil]; When I log my text I get below for attached screenshot <font color="#ff66b2">dasdasd asd asd asd sa asda </font> <font