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.
Related
I would like to implement rich format text views as are demonstrated in Instagram. Following is a screenshot.
Specifically, my goals are:
Words at different positions of the same text view may have different font sizes, font colors and font styles(bold, italic, etc).
Touch events (long press, touch down, etc) can be detected in the delegate callbacks. Information (which word is touched, whether it's a long press or a touch down, etc) can be gathered in such callback methods.
Big frameworks like Three20 are out of the question. Small, independent libraries are highly preferred. Low level Cocoa Touch APIs may also be OK if it won't take me more than a few days to wrap them up.
Any suggestions are highly appreciated.
NSAttributedString lets you do this. If you have access to the iOS 5 developers cookbook by Erica Sadun there is a recipe for a wrapper around NSMutableAttributedString which makes it simple to add text piece by piece, changing attributes as you go.
Here is the source on Erica's githuib
Finally found an ideal open source solution (supports both iOS 5.0 and iOS 6): the OHAttributedLabel, which is capable of both rich format and touch handling.
https://github.com/AliSoftware/OHAttributedLabel
I'm new to iPhone development, and I have a question on how to create a view for my application.
The view should display a problem (using formatted/syntax highlighted text), and multiple possible answers. The user should be able to click on an answer to validate it.
Currently, I am trying to use a UITableView embedding UIWebView as contentView. That allows me to display formatted text easily.
The problem is that it is a real pain to compute and adjust the height of the cells. I have to preload the webview, call sizeToFit, get its height, and update the cell accordingly. This process should be done for the problem and the answers (as they are HTML formatted text too).
It's such a pain that I am planning to switch to something else. I thought using only a big UIWebView and design everything in HTML. But I looked at some articles describing how to communicate between the HTML page and the ObjectiveC code. This seems to involve some awful tricks too...
So... that's it, I don't really know what I should do.
I guess some of you dealt with such things before, and would provide some greatly appreciated tips :)
The catch here is that the iPhone API does not yet support NSAttributedString so you can't just set the text to appear as you would like in a textview.
I saw one work around which essentially used individual UILabels to represent each attribute run. (Can't find the link now.) They used NSString UIKit extensions to calculate the position of the strings on the view and then used that to position the labels.
Another work around would be to draw the strings with their attributes to a UIImage and then just display the image. That would be the easiest solution I think.
In either case your going to have to basically recreate the data structure of an attributed string.
NSAttributedString does a lot of work for us. We really miss it when it is gone.
I want to implement a view in an iPhone application that is essentially like a rich text document. I need it to be click-editable, and I'd like to be able to embed graphic objects (either an overlaid view object, or manually drawn in graphic) with the text wrapping around. much like you would expect in a word processor. That's about the minimum functionality needed. Changing font for certain text would be a bonus (bold, size, etc).
UITextView would be a great start for me if it supported media like graphics embedded.
I'm still very new to Cocoa and Obj-C. Where should I start?
UITextView will not be nearly sufficient -- it has a very well-defined and simple functionality. That is an extremely complicated thing you're trying to do, wrapping text around an image -- you'll have to use to manually render the text in your drawRect method and do some very complex collision detection and calculate the string sizes etc. It's do-able, but extremely complicated.
Now, if you don't want the text to hug the image, but rather have the two appear on distinct lines, then you could fake this with a UITextView, then a UIImageView, then a UITextView, manually changing size and offset of each as the text changes...but this is a cheap hack and not exactly extensible. It could be sufficient for your needs, however.
UITextView does not allow rich formatting (bold, italics, different sizes, colors...), so that too would require a custom sort of text view.
Basically, it's a pretty big undertaking. If you're really committed, I recommend what Alex said -- get very, very comfortable with UIKit and Objective-C and iPhone coding in general. Then research how to make a rich text editor in other languages more suited to the functionality, and try porting that to the iPhone.
Hope this doesn't sound too discouraging. It's possible, but it won't be easy. And always bear in mind that the iPhone is a phone. Is it really the best platform for your application?
You might start with a much smaller, unrelated project. A viewer is no problem — start with UIWebView, which can render a RTF document for viewing. Creating a document editor, however, is no small task.
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.
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.