Emulating UITextView text rendering with CoreText? - iphone

I'm attempting to create a TextView with the ability to highlight various words. I don't want to use the attributedText property in iOS 6 because I want to support iOS 5 and because of other issues (see here for example: UITextView attributedText and syntax highlighting).
So the only option I've got is to use CoreText. I found this project: https://github.com/KayK/RegexHighlightView. What it basically does is overrides the drawRect method of the UITextView and draws the contents with CoreText. The problem is that the Core Text rendering is different from UITextView rendering - which leads the cursor to be in a wrong place (gets worse as the text gets longer).
Is there any hope to fix this issue? Maybe some other clever approach?

Try this:
https://github.com/mattt/TTTAttributedLabel
TTTAttributedLabel supports iOS 4.0 and higher.

Try EGOTextView. Or you can do the webView way. (UITextView actually does this under the hood mostly)

Related

Premade safari like UITextField and UITextView

HI all,
Are there any premade/ready to use UITextFields and UITexView which have the toolbar(for hiding the keyboard) and autoscrolling features just like the one safari(iphone) have.
I know its possible to make it custom but I wanted to use a builted one due to lack of time.
Thanks,
No, not in the iOS SDk for iPhone.
There is the Three20 library which has some nice controls. This a bit big for just those controls.

UILabel text on iPad not resizing

I have iOS 4.3 installed on my iPad. I'm noticing that my text in my UILabels is not resizing. In other words, I'm adding letters, but it's just truncating. Same settings work find on iPhone also running 4.3. I'm perplexed. I've made certain that "Adjust to fit" is checked on the label properties. I've even set it in the code with .adjustsFontSizeToFitWidth and even tried calling sizeToFit.
None of these let the text resize.
Does anyone else have this problem?
Any ideas?
My next solution is going to use this: Check if label is truncated to try and manually resize the label text.
I finally figured it out. I'm using OHAttributedLabel. I had intended to do some things with color in my labels and have not yet gotten around to it. It finally dawned on me that was the only difference from previous iPad versions and from the iPhone version (I never even thought to look at the class). Turns out this OHAttributedLabel class does not support resizing yet.
Sorry for wasting everyones time. Maybe someone else will someday find this useful.
Try using CGSize eLabelSize = [yourLabel.text sizeWithFont:yourLabel.font]; to get the size of the label and then you can simply modify the yourLabel.frame.size property with eLabelSize.
This worked for me in case of iPhone.
Hope this works for you if yes do communicate..... :)
Maybe it only appears as if the label is truncating text because the label frame runs out of the bounds of its parent view, which clips to its bounds? Verify the frame of the label and the autoresizing mask.
Also, the minimumFontSize property is set low enough?

CFAttributedString on the iPhone

Is it possible to use the CFAttributedString type to draw formatted text on the iPhone? I see it in the documentation, but I can't figure out how to actually draw it to a context.
Three20 has a formatted text field. Basically Joe Hewitt implemented a light HTML interpreter to render the text, so it is much faster than a webview. fast enough to be used in a tableview.
The workaround is to use a WebKitView. Format your text as HTML and display in a mini web view at whatever size you need.
Actually, it turns out this is answered in the iPhone documentation, I just didn't read it carefully enough:
iPhone OS Note: While Core Foundation on iPhone OS contains CFAttributedString, there are no additions to the APIs in UIKit to add specific attributes such as font, style, or color, and there are no APIs to draw attributed strings.
There you go, no free formatted text. Bummer.
Kyle
There's an undocumented method on TextView to pass in HTML formatted text (it's really a kind of web view underneath). A number of apps in the store make use of it, just make sure your app works without it there.

Why No NSAttributedString on the iPhone?

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 table view cells really heavy, and really hurts performance.
Anyone got any ideas? Any genius's working on an opensource alternative to NSAttributedString?
NSAttributedString is now on the iPhone as of 4.0 Beta
The current from the documentation recommended way to do it is to use a UIWebView.
I don't know if this is still relevant for you, but Joe Hewitt has published his three20 library in the meantime, which contains TTStyledText and TTStyledTextLabel to fit your needs.
I'm afraid you're going to have to roll your own. If you're getting bogged down when doing table drawing, I'd probably switch to raw Quartz calls; try and dump all your drawing into a single view, and do all your complex string drawing within it. NSAttributedString is handy, but I don't think it's using all that much special AppKit-mojo to get much better performance than straight string drawing calls.
The answer above indicates that NSAttributedString is available on OS 4.0 and above, but per the documentation it's available on 3.2:
NSAttributedString Class Reference
Inherits from NSObject
Conforms to NSCoding, NSCopying, NSMutableCopying, NSObject (NSObject)
Framework /System/Library/Frameworks/Foundation.framework
Availability Available in iPhone OS 3.2 and later.
Companion guide Attributed String Programming Guide
Declared in NSAttributedString.h
Another approach would be to use a UIWebView with HTML. Or, if you are daring you can make use of the undocumented setHTML method on UITextView to set small snippets of formatted text...
To me this seems more like the future of where supporting formatted text will go, some way to leverage Webkit in outputting formatted strings.

Display formatted text on the iPhone

I need to display text in an iPhone application, something like the way a book is displayed, for example:
Heading
Sub heading
The actual text of the book. Blah. Blah. Blah.
How would I go about doing that? I've found the UITextView and UITextField and UIScrollView objects, but I can't figure out how to use them properly... Any suggestions?
I hope that makes sense...
You could use HTML in a UIWebView. Or layout a view with multiple UILabels set for particular fonts/sizes/properties. Then use a UITextField for the rest of the unformatted text.
In desktop cocoa you can use attributed strings, but I don't think those are available for the iphone.
As Ryan said, you can use HTML with a WebView, but if you want to stick to native text drawing, you're going to have to drop down to CoreGraphics and draw all the text by hand. This is a lot of work, but if done right it will be more efficient and have a lower memory footprint than using WebView.
Edit: just took a look at your requirements again, and if only the heading and subheading require style changes, then I would recommend just using separate UILabels for those. You can also call -sizeToFit on the UILabels after assigning their text/font properties so they'll fit their text, which will let you handle wrapped headers/subheaders.