nsattributedstring

Top-aligning text of different sizes within a UILabel

丶灬走出姿态 提交于 2019-12-02 21:24:20
How to top-align text of different sizes within a UILabel? An example is top-aligning smaller-sized cent amount with larger-sized dollar amount in price banners. UILabel in iOS6 supports NSAttributedString which allows me to have text of different sizes in the same UILabel. However it doesn't seem to have an attribute for top-aligning text. What are the options to implement this? It seems to me that providing a custom drawing logic to do top-alignment based on a custom attributed string key might be best but I have no idea how to go about it. I was able to achieve your desired result using a

NSAttributedString ignores Autoshrink and numberOfLines for UILabel (iOS 6)

懵懂的女人 提交于 2019-12-02 17:31:06
I have UILabel with number of lines = 2 system font size = 15 minimum font size = 8 Line break mode - Truncate tail When I set long text which have type NSString for UILabel it works fine and shows multiline text (scaled if needed). When I am trying to set text with type NSAttributedString it ignores minimum font size and Autoshrink so I see one line text with maximum font size. Is it possible to solve this problem Looks something like this (Label size is const) ----------------------- | normal NSString Text| | very very long ... | ----------------------- ---------------------------

iPhone CoreText: Find the pixel coordinates of a substring

自古美人都是妖i 提交于 2019-12-02 16:01:11
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/library/ios/#documentation/Carbon/Reference/CTRunRef/Reference/reference.html ) which looks promising, but

Adding underline to UILabel attributed string from the storyboard fails

谁都会走 提交于 2019-12-02 15:22:32
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. Here is the solution in storyboard: Open the Attribute Inspector (make sure label is selected), Change the dropdown value from 'Plain' to

Attributed string with custom fonts in storyboard does not load correctly

梦想与她 提交于 2019-12-02 14:20:35
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? The fix for me was to use an IBDesignable class: import UIKit @IBDesignable class TIFAttributedLabel: UILabel { @IBInspectable var fontSize: CGFloat = 13.0 @IBInspectable var fontFamily: String = "DIN

Toggle selectedRange attributes in UITextView

断了今生、忘了曾经 提交于 2019-12-02 11:01:50
I have created a button that I want to check if text is selected then if so toggle bold and unbold over the selectedRange when tapped. At the moment my code will just change the selectedRange to bold and I can't undo it or check if there is a selection. How can I achieve this? func bold() { if let textRange = selectedRange { let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.bold)] noteContents.textStorage.addAttributes(attributes as [NSAttributedString.Key : Any], range: textRange) } This might do the trick: func toggleBold() { if let textRange

Getting the range of links in attributed string

好久不见. 提交于 2019-12-02 10:29:54
I would like to find the range of links in attributed text, so I could apply custom underline only to the relevant words. At the moment, the underline is under all of the text. I want it to be only under the links. The code is a bit complex as the requested underline is super customised. import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let text = "random text <a href='http://www.google.com'>http://www.google.com </a> more random text" let storage = NSTextStorage() let layout = UnderlineLayout() storage.addLayoutManager(layout) let

how to set attributes in NSAttributedString in iOS?

随声附和 提交于 2019-12-02 05:03:35
I'm trying to set the infamous NSFontAttributeName property of an NSAttributedString in iOS but it just doesn't seem to work: first off, none of the NS constants seem defined for iOS I read somewhere that I could instead work around it by passing the CoreText consts instead. Fine... but still, The attribute expects an NSFont and I'm stuck with UIFont or CTFontRef, neither of which seems to work: this doesn't work: CTFontRef ctFont = CTFontCreateWithName((CFStringRef)[UIFont boldSystemFontOfSize:16].fontName, [UIFont boldSystemFontOfSize:16].pointSize, NULL); [myAttString addAttribute:(NSString

NSMutableAttributedString not working in tableviewcell with a specific range

与世无争的帅哥 提交于 2019-12-02 04:54:01
问题 I have followed this question but didn't solve my problem. I have a tableview in ViewController , tableviewcell have a label. I want to set attributedString with NSStrikethroughStyleAttributeName . If i am setting complete string as a strikethrough it works but if i am setting as partial it doesn't works. Below code for success result let strOriginalPrice = "my price" let strdiscountedPrice = "discounted price" let strPrice = strOriginalPrice+" "+strdiscountedPrice let attributeString:

Get text range of link in UITextView

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 02:10:28
I have a UITextView that contains a link and some other text. The link detection is enabled and working (iOS8). However, I am at loss to find the range of the link within the textView. The idea is that the text is scanned (after link detection is done) and the link preloaded into a web view, so that when a user taps on an element somewhere, the web view can reveal itself. How can I get the range of the detected link within a UITextView? A way to do it, is to use enumerateAttribute:inRange:options:usingBlock: , where from my example attr is [yourTextView attributedText] . __block