Workaround Sought for: iOS 6 NSAttributedString Descenders Block Underline - iphone

I am using NSAttributedString (and NSMutableAttributeString) for writing to a PDF via UIGraphicsBeginPDFContextToFile. When I output an attributed string with underline attributes, the underline is broken when a letters descender goes over underline.
Here is a sample (screen capture) showing the current output:
And here is the code that builds that sample attributed string:
NSAttributedString* ftype =
[[NSMutableAttributedString alloc]
initWithString:#"Dangerous"
attributes:#{
NSParagraphStyleAttributeName:pstyle,
NSFontAttributeName:[UIFont fontWithName:#"TimesNewRomanPS-BoldMT" size:48.0],
NSForegroundColorAttributeName:[UIColor redColor],
NSUnderlineStyleAttributeName:[NSNumber numberWithInt:NSUnderlineStyleSingle]
}
];
My client's and my preference is that the underline be continuous, and ideally shifted below the descender.
Manually drawing the underline is difficult as we would have to computer the text position of the words after layout (sometimes the text is not as simple as the example above).
Does anyone have a fix to put the underline either a) lower or b) make it continuous?
Thanks in advance.

I know this was old and after searching found no answers. The solution I found is in the dictionary you pass in for attributes to NSAttributedString initWithString:attributes: add this
NSExpansionAttributeName : #-0.0001f
This scales the glyph, and it seems even with this small amount the underline doesn't get cut (well it didn't with the 13pt font I was using). Though this doesn't seem like the intended use of NSExpansionAttributeName, but couldn't find another solution.

Related

How to replace multiple glyphs in Swift NS/UITextView with a single icon

I'm struggling with this one for a while now. Its's about rendering markdown text in UI/NSTextView.
In the app "Bear" for Mac/iOS you can add a markdown link like [This is a link](https://...com) and it will hide the full URL and show a link symbol instead.
Here is a screenshot:
I know how to change colors using NSAttributedString and I know how to hide glyphs by overriding NSLayoutManagerDelegate.
There I override the shouldGenerateGlyphs func and change the properties of the glyphs to .null to hide them. Here I can also 'replace' a single glyph with something else by creating a CGGlyph(exactly: Int) and setting the glyph pointer to this in the same method where you can hide glyphs. But you can only replace a single glyph, not multiple.
While the above might work, it feels very hacky. I would need to hide all URL string glyphs, except the first, and turn that into a symbol somehow.
Is there any way to say something in the sense of mergeGlyphs(in range: NSRange, to glyph: CGGlyph) or to combine multiple glyphs to one or any other idea this could be achieved?

text-align-last css property Swift Equivalent

What is the swift Equivalent for 'text-align-last' css property?
I prefer a codeless solution.
Here is what i have done and what i get:
The last line (sometimes the only one) is aligned to the left, which is inconvenient.
You need to change the label's text from Plain to Attributed, then you can paste any string and the alignment, as well as other attributes, will hold.
So basically any text style that can create on word processor application can be used here.
In the following example I've used pages (Mac application) to edit the text format as I liked and copied it to the label text box in Xcode.
Here is a picture of the simulator running the app:
While it is not possible to apply different aligns on a single label, if the last(and sometime only) line should be aligned to the right why not align the entire label to the right?
EDIT
If that doesn't fix you problem then I don't think it's possible to resolve without code.
In the case you do decide to write some code you could look into determining which is the last line of your string (maybe: How to get text from nth line of UILabel? ) and try to apply different formatting with AttributedString.
If that works then you can always subclass UILabel and override func layoutSubviews() to calculate this automatically for you. This way you won't have to think about it again!

Howto change from proportional to tabulated digits in menu items?

I want to change from proportional to tabulated digits in menu items? It's possible?
Menu without tabulated digits
You need to use the monospaced font like this:
label.font = UIFont.monospacedDigitSystemFont(ofSize: 17, weight: UIFontWeightRegular)
Obviously you can specify whatever font size and weight you want (or get them from the current font descriptor).
You can set the attributedTitle property of a NSMenuItem and provide whatever custom alignment or formatting you want through an NSAttributedString
Good examples of how to do string alignment in an attributed string can be seen here:
Attributed Text Center Alignment
Here is an answer that shows how to add tab stops to an attributed string:
How do I add tab stops to an NSAttributedString
You can even create an attributed string from RTF or HTML:
Creating an NSAttributedString from HTML (or RTF)

iPhone SDK - UITextView text color split

I have a UITextView that I would like be be able to change the color for. But I would like to be able to change the color only after 50 characters. I can use UITextViewDelegate methods to count the characters, but I'm not sure how to change the color after that.
For instance, when I have a 100 character phrase within the UITextView, I would like the first 50 characters to be blue, and the last 50 characters to be red.
Is this possible? Any advice on how to achieve this?
Many thanks,
Brett
It is possible, but not via native UITextView or so
read more here:
http://www.cocoanetics.com/2011/01/rich-text-editing-on-ios/
http://www.scoop.it/t/iphone-and-ipad-development/p/104023127/rtlabel-rich-text-formatting-for-ios-using-html-like-markup-coding-for-mobile
http://iphoneincubator.com/blog/windows-views/display-rich-text-using-a-uiwebview
like #Nekto said
You can set only one color for all characters in one text view, using textColor property. If you want different colors in one sequence of characters, you can use, for example, UIWebView and html tags.

NSString drawInRect with UILineBreakModeTailTruncation doesn't appear the "..."

I am trying to use this method to draw a string into a custom UITableViewCell.
[self.text drawInRect:TEXT_RECT withFont:font lineBreakMode:UILineBreakModeTailTruncation alignment:UITextAlignmentLeft];
The problem is that if the text is too long, the text is actually tail truncate but it doesn't display the "..."
If I use the drawInPoint
[self.text drawAtPoint:CGPointMake(60, 0) forWidth:200 withFont:font minFontSize:15 actualFontSize:nil lineBreakMode:UILineBreakModeTailTruncation
baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
Then, I get the "..." but now it make all of my text one line, so the quite long text will get truncated too soon. For example:
If I have the text "Hello all, here is my first book". If I use drawInRect, then I can display it in 2 lines, but if I use drawAtPoint, I only see the first line like : "Hello all, here ..."
So, any help to make either method work will be appreciated
UILineBreakModeTailTruncation only truncates the last line of text. Is TEXT_RECT big enough to hold multiple lines of text? What appears to be tail truncation might just be a rect that isn't tall enough.