nsattributedstring

NSAttributedString add text alignment

空扰寡人 提交于 2019-11-27 09:27:07
问题 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.spec = kCTParagraphStyleSpecifierAlignment; setting.valueSize = kCTCenterTextAlignment; CTParagraphStyleSetting settings[1] = { {kCTParagraphStyleSpecifierAlignment, sizeof(CGFloat), &setting}, }; CTParagraphStyleRef paragraph = CTParagraphStyleCreate(settings, sizeof(setting));

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

烂漫一生 提交于 2019-11-27 08:51:41
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 done automatically but I guess not. I have looked at the UImage class reference but this image doesn't seem to be set in a UIImageView so no access to a frame property. Here's a screenshot of what I currently have: In an ideal world, I would also like to implement a way to scale up the image based upon user input (such as increasing the font size). Any ideas on how to achieve this? thx edit 1 here

Find attributes from attributed string that user typed

旧巷老猫 提交于 2019-11-27 08:47:00
In my app the User will type anything that he wants and he can make the text Bold, Italic and Underline. I am able to do the Text - Bold, Italic and Underline. But I need to find which Text (or Character) is Bold, Italic and Underline from the attribute string that user typed . Any types of link, tutorial, code will be great help. EDIT : I need the result like this... if character 6 is Italic then print Italic. if character 14 is Bold then print Bold. I need to convert all the text from Calibri font family to HelveticaNeue font family without changing it's attribute. You can enumerate the

Calculate the range of visible text in UILabel

ぐ巨炮叔叔 提交于 2019-11-27 08:06:53
问题 I have an UILabel with fixed size, the text that I set into this UILabel can be 200, 5 or 500 characters long. What I want to do is to calculate how much visible text can I put into this UILabel with the current UILabel size. Why I want to do that? Because I want to add a ...Read more text at the end of the text, but not at the end of the whole text, just at the end of the visible text in the UILabel . Thanks in advance. 回答1: So I've created a method which returns the current visible string

Replace substring of NSAttributedString with another NSAttributedString

泪湿孤枕 提交于 2019-11-27 07:56:41
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:withString: for 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

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

∥☆過路亽.° 提交于 2019-11-27 07:46:57
Is it possible to apply both stroke and fill with an NSAttributedString and a UILabel ? Yes, the key is to apply a Negative value to the NSStrokeWidthAttributeName If this value is positive you will only see the stroke and not the fill. Objective-C: self.label.attributedText=[[NSAttributedString alloc] initWithString:@"string to both stroke and fill" attributes:@{ NSStrokeWidthAttributeName: @-3.0, NSStrokeColorAttributeName:[UIColor yellowColor], NSForegroundColorAttributeName:[UIColor redColor] } ]; Thanks to @cacau below: See also Technical Q&A QA1531 Swift version: let attributes =

Why No NSAttributedString on the iPhone?

这一生的挚爱 提交于 2019-11-27 07:17:20
问题 Does anyone know what made Apple leave out NSAttributedString when turning AppKit into UIKit? The reason I ask is that I would really like to use it in my iPhone app, and there appears to be no replacement or alternative than doing it myself... It is possible to have mixed font attributes on a string - it's just a hell of a lot of work to to achieve something similar that was possible with a few lines of code with NSAttributedString. Also, doing all this extra drawing code myself makes my

Convert NSAttributedString into Data for storage

蹲街弑〆低调 提交于 2019-11-27 06:59:43
问题 I have a UITextView with attributed text and allowsEditingTextAttributes set to true . I'm trying to convert the attributed string into a Data object, using the following code: let text = self.textView.attributedText let data = try text.data(from: NSMakeRange(0, text.length), documentAttributes: [:]) However, this is throwing the following error: Error Domain=NSCocoaErrorDomain Code=66062 "(null)" Any ideas what this error means or what could cause this? I'm on the latest Xcode and iOS.

Displaying text one character at a time in swift 2.0

流过昼夜 提交于 2019-11-27 06:25:40
问题 New to Swift 2.0; trying to learn it, interested in animation and tried to write some code to display a message on screen one character at a time. I wrote this, it works, but I cannot help but could I not do something with CALayers perhaps and/or alpha values? Or some animate gizmo, something more swift worthy; this feels & looks kinda clunky, sort 1977 really. import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup

Copy NSAttributedString in UIPasteBoard

坚强是说给别人听的谎言 提交于 2019-11-27 04:52:59
How do you copy an NSAttributedString in the pasteboard , to allow the user to paste, or to paste programmatically (with - (void)paste:(id)sender , from UIResponderStandardEditActions protocol). I tried: UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard]; [pasteBoard setValue:attributedString forPasteboardType:(NSString *)kUTTypeRTF]; but this crash with: -[UIPasteboard setValue:forPasteboardType:]: value is not a valid property list type' which is to be expected, because NSAttributedString is not a property list value. If the user paste the content of the pasteboard in my app, I