nsattributedstring

NSAttributedString click event in UILabel using swift

若如初见. 提交于 2019-12-03 03:13:37
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. 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:shouldInteractWithURL:inRange:interaction: method to achieve this, ex: class ViewController: UIViewController,

iPhone CoreText: Find the pixel coordinates of a substring

≡放荡痞女 提交于 2019-12-03 02:31:23
问题 Here's a screenshot of the twitter app for reference: http://screencast.com/t/YmFmYmI4M What I want to do is place a floating pop-over on top of a substring in an NSAttributedString that could span multiple lines. NSAttributedString is a requirement for the project. In the screenshot supplied, you can see that links are background-highlighted, so it leads me to believe that they're using CoreText and NSAttributedStrings. I also found something called CTRunRef ( http://developer.apple.com

CGRect for selected UITextRange adjustment for multiline text?

六月ゝ 毕业季﹏ 提交于 2019-12-03 02:17:34
I've used this answer in order to create a CGRect for a certain range of text. In this UITextView I've set it's attributedText (so I've got a bunch of styled text with varying glyph sizes). This works great for the first line of text that's left aligned, but it has some really strange results when working with NSTextAlignmentJustified or NSTextAlignmentCenter . It also doesn't calculate properly when the lines wrap around or (sometimes) if there are \n line breaks. I get stuff like this (this is center aligned): When instead I expect this: This one has a \n line break - the first two code bits

How to make NSStringDrawingContext shrink text?

强颜欢笑 提交于 2019-12-03 02:09:59
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]; paragraphStyle.lineBreakMode = NSLineBreakByClipping; NSDictionary *attributes = @{

UITextView with custom font not working when text set to “attributed”

[亡魂溺海] 提交于 2019-12-03 01:55:07
I have a UITextView with some text that came from a .rtf (pasted directly onto Xcode) The context contain only one custom font (Futura Book BT 11.0) If I set the "text(attributed)" property to "plain"= The custom font appear properly from the storyboard and from the app If I set the "text" property to "attributed"=. The custom font appear properly from the storyboard BUT not from the app. As my goal was to have a text with multiple font working, how to have the attributed property to work with custom fonts? (Swift) Thanks! The custom font appear properly from the storyboard BUT not from the

Adding underline to UILabel attributed string from the storyboard fails

旧巷老猫 提交于 2019-12-03 01:48:48
问题 From the storyboard I select the UILabel in question Then in Attribute Inspector > Label > [I choose] Attributed Also in Attribute Inspector > Label > Text> [I select the content] Then I click on the font icon and choose underline Basically, any change that I select from the Fonts window that pops up does not take effect. Has anyone ever successfully add underline from the storyboard? Note: I already know how to do this in code. I want to avoid adding code. 回答1: Here is the solution in

Attributed string with custom fonts in storyboard does not load correctly

点点圈 提交于 2019-12-03 00:58:54
问题 We are using custom fonts in our project. It works well in Xcode 5. In Xcode 6, it works in plain text, attributed string in code. But those attributed strings set in storyboard all revert to Helvetica when running on simulator or device, although they look all right in storyboard. I'm not sure if it's a bug of Xcode 6 or iOS 8 SDK, or the way to use custom fonts is changed in Xcode 6 / iOS 8? 回答1: The fix for me was to use an IBDesignable class: import UIKit @IBDesignable class

How to get NSRange(s) for a substring in NSString? [duplicate]

陌路散爱 提交于 2019-12-03 00:49:19
This question already has answers here : How to get all NSRange of a particular character in a NSString? (4 answers) NSString *str = @" My name is Mike, I live in California and I work in Texas. Weather in California is nice but in Texas is too hot..."; How can I loop through this NSString and get NSRange for each occurrence of "California", I want the NSRange because I would like to change it's color in the NSAttributed string. NSRange range = NSMakeRange(0, _stringLength); while(range.location != NSNotFound) { range = [[attString string] rangeOfString: @"California" options:0 range:range];

Add a tap gesture to a part of a UILabel

99封情书 提交于 2019-12-03 00:32:11
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 string above? Thanks! I think, the best way is adding the UIGestureRecognizer to your UILabel and validate

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

房东的猫 提交于 2019-12-02 21:39:24
I've created a method that takes a NSAttributedString and I'm looking to dynamically create a subview and label to put the string into. Because attributes like font and size need to be determined to correctly determine the size of the label, I need to determine if it is possible to iterate through values and ranges that have been applied to the attributed string? I understand that I could pass the attributes separately, but for sake of reusability, i'd like to be able to pass as few parameters to the method as possible. Apple expects you to use enumerateAttributesInRange:options:usingBlock: .