Getting Justified Text in UITextField - cocoa-touch

How do I get justified text with UTTextField. It does have an textAlignment property. But the UITextAlignment constant only has left, right, and center justification.
What I am seeking is the Justified text common in word processing app with text flush with both left and right edges. This is a read only text field.
I have seen it in few iPhone apps. So it seems I am missing something.

It's a bit of an overkill maybe, but one way seems to be to use a WebView for it and style the text with CSS.

UILabel and UITextField do not support full-justified text. If you want it for a UITextField, you would have to create a subclass of UITextField and override drawTextInRect:, splitting the text into words and using sizeWithFont: to figure out how to space them along each line.
I cannot imagine what would be worth the trouble.

Related

UITextView vs UILabel+ScrollView

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 :)

Pixate background-image no-repeat?

I am trying to put a background image into a text-field (it's just a search icon). And it is tiling the image in the text field. Is it possible to turn off the repeating of the image? i don't see anything like that, i'm seeing padding and position for background images but nothing to turn off the tiling of the image. is this not possible in pixate yet?
thanks!
You should be able to set background-size to the size of your text-field so it won't have anything to repeat.
I battled this same problem, and in the end, I found I got what I wanted by using multiple objects. Even if you get the search icon to work right, you're probably going to notice the UITextField doesn't pad the text away from the icon.
Anyway, here's how I ended up doing it so that I had full control over the style.
So you can put your search icon in the UIImageView and then define your text field's style (border, background color, etc.) with the UIView. The UITextField ends up being plain white/transparent, and positioned so that its text doesn't overrun the search icon.
I hope that helps.

Adding a header and text to UITextView

I thought this would be simple but it's proving not to be.
I have some text that I need to display in my app. What is the best way to present it? Because I want the text but also want a header - the header will be larger. I know UITextView only allows 1 type of font/size.
Example of what I want
Example Header - Bold size 22
Text - size 16
How do I do this?
If your text is not editable, I think you can think about UILabel instead of UITextView, and you can use more complicated view hierarchy, not just an UITextView or UILabel.
If you insist using UITextView, I think you can refer to NSAttributedString.

Objective C - UITextView letter spacing and vertical spacing

How can I set the letter spacing and vertical spacing for a UITextView?
Trying to do it from nib if possible but if not, is there a property I can set through code?
Thank you,
Tee
There is no property within the UITextView to explicitly set the letter spacing, or vertical spacing - with the native controls it can't be done.
If you want to do this you're going to have to roll your own. There's a SO thread about changing the UILabel/UIFont letter spacing which ought to provide you with a direction to go in.
With that said, I have to ask the question why you want to do this? Apple is very specific about it's interface elements, and my thought is that tweaking a UITextView is going to be very off putting to your users.
As gavin has said, really; if possible you could change to a UILabel and set the lineBreakMode property, in conjunction with the contentSize property of the label's frame to partly achieve some light modification.
You could also replace spaces with a number of spaces for example from a string object, but again as has been said, I wouldn't advise tampering too far with this, especially if its going to be a public appstore project.
Good luck!
Well if you are looking for line height you can get like this.
yourUITextView.font.lineHeight
This would give you line height according to the current font size. This works perfectly in iOS 8. Not sure about backward compatibility.

Is there a way to change color of just part of a string inside a UILabel?

Say I wanted to have the first six characters in black and the next six characters in blue, then the last 20 characters in black of a UILabel. Is this possible?
Thanks!
In Big Cocoa you would use an NSAttributedString in a NSTextField or NSTextView. In Cocoa Touch you use a UIWebView to display rich text.
Zynga has done a lot of the heavy lifting for you on this one, check out Font Label on GitHub.
As Darren said, NSAttributedString is not supported on the iPhone but you could use a UIWebView.
Another possible solution might be to draw the text using NSString's -drawAtPoint:withFont: method (or similar). You could subclass UILabel and do the drawing in -drawRect:.
First, set the color to black and draw the first six characters. Note the width that -drawAtPoint:withFont: returns, and use it to calculate the starting point for the next six characters. Change the color, draw those characters, and then repeat the same for the remainder of the string.
Note that -drawAtPoint:withFont: doesn't do line breaks and stuff like that, so it could quickly get more complicated than what I've described.
Have a look at the NSString UIKit additions.
Give TTTAttributedString a try. It's a drop-in replacement for UILabel that renders NSAttributedStrings like a champ.
You want an NSAttributedString.