nsattributedstring

how to change characters case to Upper in NSAttributedString

落爺英雄遲暮 提交于 2019-12-03 13:08:14
I want to convert NSAttributedString containing RTFD to uppercase without losing attributes of existing characters and graphics. Thanks, EDIT: @fluidsonic Is correct that the original code is incorrect. Below is an updated version in Swift, that replaces the text in each attribute range with an uppercased version of the string in that range. extension NSAttributedString { func uppercased() -> NSAttributedString { let result = NSMutableAttributedString(attributedString: self) result.enumerateAttributes(in: NSRange(location: 0, length: length), options: []) {_, range, _ in result

Display NSAttributedString using CoreText

房东的猫 提交于 2019-12-03 13:00:02
问题 I have heard that I can display a NSAttributedString using CoreText, can anyone say me how (The simplest way)? Please, don't answer with CATextLayer or OHAttributedLabel. I know that there are a lot of questions about this in this forum, but I haven't find the answer Thanks!! 回答1: I think that the simplest way (using Core Text) is: // Create the CTLine with the attributed string CTLineRef line = CTLineCreateWithAttributedString(attrString); // Set text position and draw the line into the

NSAttributedString click event in UILabel using swift

て烟熏妆下的殇ゞ 提交于 2019-12-03 12:59:53
问题 Suppose I have an AttributedString : "Already have an account? Sign in!". I am placing this String in UILabel. Now when a user clicks on "Sign in!", current viewController should go to another viewController or some function should be called while clicking on sign in. Any code or suggestion should be fine. 回答1: There's no need to use a separate gesture recognizer as some of the answers state. Instead, you can use attributed text in combination with the UITextViewDelegate 's textView

iOS - Using TTTAttributedLabel to set two color text

冷暖自知 提交于 2019-12-03 12:47:16
问题 I'm creating iOS app that has a label. I want to set two colors. One for first part and other color for remaining part. I've seen some messages in Stack over flow that, TTTAttributedLabel has the ability to set more than one color to text. My text will be like "ABC > def". For "ABC", i want to set brown color and for "def", i want to set white color. How can I set that? 回答1: NSString* text = @"ABC > def"; attributedLabel = [[[TTTAttributedLabel alloc] initWithFrame:frame] autorelease];

UILabel attributedText with multiple line break modes

僤鯓⒐⒋嵵緔 提交于 2019-12-03 12:21:08
问题 I have a requirement of showing a UILabel with text that has two different styles (different colours, parts of the text bolded). This is solved easily enough by using the attributedText-property. My problem is that the text may or may not be longer than what I can fit in my label. When using plain text everything works the way I want it to. The text is word wrapped to fit the number of lines in the label and the tail is truncated if/when the text is longer than can be shown in the label. When

How to make NSStringDrawingContext shrink text?

浪子不回头ぞ 提交于 2019-12-03 11:43:23
问题 I'm trying to use the attributed string API of iOS 6 to calculate the size of text and shrink the font size if necessary. However, I can't get it to work as the documentation says. NSString *string = @"This is a long text that doesn't shrink as it should"; NSStringDrawingContext *context = [NSStringDrawingContext new]; context.minimumScaleFactor = 0.5; UIFont *font = [UIFont fontWithName:@"SourceSansPro-Bold" size:32.f]; NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];

How can I accurately detect if a link is clicked inside UILabels in Swift 4?

我怕爱的太早我们不能终老 提交于 2019-12-03 11:23:49
Edit See my answer for a full working solution: I managed to solve this myself by using a UITextView instead of a UILabel . I wrote a class that makes the UITextView behave like a UILabel but with fully accurate link detection. I have managed to style the links without problem using NSMutableAttributedString but I am unable to accurately detect which character has been clicked. I have tried all the solutions in this question (that I could convert to Swift 4 code) but with no luck. The following code works but fails to accurately detect which character has been clicked and gets the wrong

Add a tap gesture to a part of a UILabel

狂风中的少年 提交于 2019-12-03 11:03:57
问题 I have a NSAttributedString like so: NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"testing it out @clickhere"]; NSInteger length = str.length; [str addAttribute:NSForegroundColorAttributeName value:[UIColor bestTextColor] range:NSMakeRange(0,length)]; The NSMutableAttributedString gets set to a UILabel like so: label.attributedText = str; How do I make a tap gesture (or something clickable) to another viewcontroller for the words '@clickhere in the

NSAttributedString initWithHTML incorrect character encoding?

橙三吉。 提交于 2019-12-03 10:57:41
-[NSMutableAttributedString initWithHTML:documentAttributes:] seems to mangle special characters: NSString *html = @"“Hello” World"; // notice the smart quotes NSData *htmlData = [html dataUsingEncoding:NSUTF8StringEncoding]; NSMutableAttributedString *as = [[NSMutableAttributedString alloc] initWithHTML:htmlData documentAttributes:nil]; NSLog(@"%@", as); That prints “Hello†World followed by some RTF commands. In my application, I convert the attributed string to RTF and display it in an NSTextView , but the characters are corrupted there, too. According to the documentation, the default

trouble saving NSAttributedString, with image, to an RTF file

微笑、不失礼 提交于 2019-12-03 10:05:47
问题 I have some output that is a very simple RTF file. When I generate this document, the user can email it. All this works fine. The document looks good. Once I have the NSAttributedString, I make an NSData block, and write it to a file, like this: NSData* rtfData = [attrString dataFromRange:NSMakeRange(0, [attrString length]) documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType} error:&error]; This file can be emailed. When I check the email all is good. Now, I'm tasked