UITextView vs UILabel+ScrollView - objective-c

Im working on a an app that requires a view Controller to display a nice amount of hebrew(non-english) text in it.
The text is an AttributedString from a Rich Text File
There are 4 things that I need the view to do.
Smooth Scrolling
Get the range of a word so I can auto-scroll to it (like scrollRangeToVisible)
Change the font smoothly (I'd like to use a UISlider to change the font)
Load quickly
In a sense Im looking for the capabilities of a e-reader just I only need one column and I want it to scroll not page.
Things I've tried and there pros and cons
Regular UITextView & Scrollview Classed as UITextView
PROS:
Easy search and auto-scroll
Quick load
Smooth font changing
CON:
Very choppy scrolling
UILabel in a ScrollView
PROS:
search and auto-scroll is not easy but manageable
Very smooth scrolling
CON:
Very choppy font switching
Slow loading
The only thing I haven't tried (that I can think of) is CoreText
Is coreText the way to go? I would rather use a UITextView if there as a way to get it to render lots of hebrew (non-english) text better.
Note: I have already tried disabling editing on the UITextView and Selectable.
EDIT 1
-(double)Height{
[layoutManager glyphRangeForTextContainer:textContainer];
NSLog(#"HEIGHT = %f",[layoutManager
usedRectForTextContainer:textContainer].size.height);
return [layoutManager
usedRectForTextContainer:textContainer].size.height;
}

I would
Create an NSTextStorage(an NSAttributedString) object with the hebrew text.
Create NSLayoutManager object, NSTextContainer. Hook them up.
Render them onto a custom UIView with a size that you can query from the NSLayoutManager like this.
Implementing scrollRangeToVisible will be a good task in itself. I would use one of these methods.
If you only have the character range and need to compute glyph range, one of these methods could help.
This won't be very easy, but a very good project :)

Related

Shadow inside text on NSTextField/NSTextFieldCel

I've been trying for a day or two to try and get a shadow to draw inside the text of an NSTextField (and making the foreground color transparent, so all you see is a shadow inside the text). I've yet to achieve a desirable and practical result. The only success I had with this was transforming the text into an NSBezerPath, subtracting it from the frame of the NSTextFieldCell and clipping using the setClip method (addClip does not suffice in this case, it leaves the background painted). Unfortunately, converting the text to an NSBezerPath makes the edges of the text look jagged and overly straightened. Is there an efficient and decent way to achieve this using CoreGraphics masks and clipping because I am unable to achieve this.
A very similar effect of what I would be looking for is in the Xcode IDE, if you don't have a debug session open and go to the debugger navigator it says "No Debug Session" in a subclassed NSTextField that draws the text with a shadow inside it. That is pretty nearly what I am after. Any insight on where to start and how to do this would be great.
A paste bin my current code (working, but sub-optimal, & non-working): http://pastebin.com/4pTv8ZWm
Have you played around with the shadow property of NSTextField in IB? You should be able to get pretty close using a slightly offset shadow effect on the text with the right blend of alpha transparency.

iOS Text Wrapping

In the app I am currently developing, I have to implement a screen which allows the user to ask a question. This screen contains a UIImageView next to the UITextView, taking up a portion of the space. It looks like the below image.
My question is, how can I wrap the UITextView text around the UIImageView, so that the text won't be in a block, but flowing around the UIImageView?
Thanks in advanced.
I don't think you can do this, unless your UITextView was a rich text or HTML editor, with the image embedded inside it. Basically what you're asking for is a non-rectangular UITextView. To the best of my knowledge, that is impossible in iOS, unless you were to create something brand new from scratch, which would be extremely complex. I have seen plenty of UILabel-style controls that can display HTML-like text, but not edit it.
EDIT: this might help: http://www.cocoanetics.com/2011/01/rich-text-editing-on-ios/
Unfortunately that's not a trivial issue as the UITextField does not provide any functionality that would be useful here.
However what you could do is to implement your own text view using UITextInput (for text input) and Core Text (for text display) and then define the drawing rectangle for Core Text (you can read more on Core Text here) from a custom CGPath that would exclude the images frame.
It does sound a bit complex (insane, perhaps), however as (nearly always) there are open source solutions that already found a solution. The OmniGroup framework contains custom text input controls based on Core Text and UITextInput. They're licensed under MIT License (well, moreless) so you should be just fine.

What is the best way to create a composite scrollable view on iOS

I need to create a scrollable composite view on iOS. That is to say, the view will contain at least one image, possibly a button, and some text (that we may wish to format with bold fonts, etc). The amount of data, and particularly the amount of text, is variable, from maybe 4 lines to maybe 100. The data is "variable" to a degree, and in particular the image and text do not come joined at the hip.
This all needs to fit in a "pane" of about 280h x 115w pixels in a portrait-only layout.
A single UITextView doesn't provide the facilities to display an image or format the text.
A UIWebView provides the ability to display the image and formatted text, but the button is a problem (not even sure if it's doable).
A UIScrollView would easily allow the image and button, and then a UIWebView could be embedded in the scroll view for the text, but then scrolling becomes a problem -- I'd like the entire view to scroll as one, without having to resize the web view to contain it's content, and without the confusion of a scrollable within a scrollable (the doc warns of "unexpected behavior").
(I'm guessing your thoughts at this point are that I want too much.)
So, any suggestions? What's the best way to get close to what I need here?
In iOS5 the UIWebView has a scrollView property, which is a normal UIScrollView. You should be able to add a UIButton as a subview of the scrollView to achieve what you want, although positioning it correctly may be a challenge. Prior to iOS5 you could cycle through the subviews of the UIWebView to find the UIScrollView with isKindOfClass...
I suggest testing with a UIWebView inside your UIScrollView. I don't see any interference in the iOS 5.0 simulator. I don't know if there are problems in iOS 4.0.
If you find that there is interference, you can prevent it by setting the web view's userInteractionEnabled property to NO, either in the nib or in code. This will prevent the web view from receiving any touches, so the user also won't be able to pinch-zoom it, follow links in it, or copy text from it.
In the web view's delegate, implement webViewDidFinishLoad: to set the web view's size and the scroll view's contentSize. For example:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGRect frame = self.webView.frame;
frame.size = [self.webView sizeThatFits:CGSizeMake(frame.size.width, HUGE_VALF)];
self.webView.frame = frame;
self.scrollView.contentSize = CGSizeMake(CGRectGetMaxX(frame), CGRectGetMaxY(frame));
}
When I did a similar thing, I had a dozen of views which I added to the UIScrollView and then calculated the frames of all the views. Granted, it was an extremely tedious work, given that some views could get hidden under various conditions. The layout code was actually pretty simple, laying out views from top to bottom, but ugly. The upshot is that it works like a charm, fast and reliably. You can even trivially wrap it in an animation block.

Chaning the background color around a PDF in a UIWebView on Device

I'm using a webview to display a PDF.
The webview displays the PDF at it's actual size which is a little smaller than the size of the webvieww itself, revealing the scroll view underneath it.
I've tried setting the Webview to opaque and setting it's background color to another color, which works fine and dandy in the simulator, but fails to change the color on the device. On the device it changes the color of the background of the view behind the scroll view, this can be seen when the PDF is pulled all the way down.
I've also tried setting all the UIView's backgrounds, by iterating through the subviews but to no avail.
I've updated a diagram to help illustrate which area I'd like to color.
Uploaded Diagram
You really shouldn't mess around with UIWebView's internals.
They can change anytime and your code might just crash on the next version of iOS.
If you need more control about pdf display, you might wanna take a look at other possibilities to show pdf, like using the CGPDFDrawPage* functions. Of course they are pretty low-level and it's a lot of work required until you can get fast page display, zooming, etc all right.

Tips on implementing a custom UITextView interface on the iPhone?

I am trying to implement a control to edit text that will display the text in multiple colors. None of the solutions I have attempted yet have been good enough.
UITextView cannot accomplish this. All of the text must be the same color.
Using CoreGraphics to draw the text does not allow the text to be selected.
Using a UIWebView, DIV and PRE tags cannot be set to contentEditable on Mobile Safari.
Currently playing with using an off-screen TEXTAREA and an on-screen DIV to show the rendered text. This works pretty well, except supporting all of these at the same time seems impossible: click-to-type, click-to-move-cursor, click-and-hold-select/copy/paste.
Anyone have any tips on this predicament?
I've been trying to find any preexisting library out there that will accomplish this in a good way, to no luck. I'm open to any ideas!
Well, just pulling an idea out of my... let's say hat.
Could you put a transparent UITextfield over a view that draws the text? If the background was clear and the text color was clear the user could not perceive it but it should still respond to all commands. As the user enters and edits text you could draw the results on the view underneath.
I think the selection would work without any modification at all. When the user selected the clear text, it should create the illusion of selecting the drawn text automatically.
Like this one? StyledText http://three20.info/gfx/overview/styledtext.png It's in Three20 .
Here is an idea. I have no idea if it would work.
If you are only using colors, and not styles, the a UIWebView with colored text might layout text in exactly the same way as a UITextView. You could put a UITextView with invisible ink (text and background fully transparent) over a UIWebView and mirror the contents with colors in the html. I assume you can do scrolling with javascript along with the colored layout.