Objective C - UITextView letter spacing and vertical spacing - objective-c

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.

Related

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.

NSTextField weird left margins

I've been looking for a solution for this one all day.
I have 4 NSTextFields (actually subclassed for a few custom operations), which all share the same X position.
The problem is, some have different styles (light, regular, bold) and might have different sizes.
What happens is that, even though the X origin is the same, the 1st letter always has a bit of (consistently different) left margins.
Please see pic: https://dl.dropbox.com/u/1977230/Screen%20Shot%202012-12-11%20at%2017.55.58.png
I want to make sure that all lines start exactly at the same point, say 100px from the left.
Any idea how to override that weird padding?
Cheers
The margin you're talking about I'm pretty sure is the lineFragmentPadding on the NSTextContainer that is used by the NSTextField.
See the NSTextContainer reference:
http://developer.apple.com/library/Mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSTextContainer_Class/Reference/Reference.html
And here's a page from the tutorial on Text Layout:
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TextLayout/Concepts/CalcTextLayout.html
It states in that article:
The typesetter makes one final adjustment when it actually fits text
into the rectangle. This adjustment is a small amount fixed by the
NSTextContainer object, called the line fragment padding, which
defines the portion on each end of the line fragment rectangle left
blank. Text is inset within the line fragment rectangle by this amount
(the rectangle itself is unaffected). Padding allows for small-scale
adjustment of the text container’s region at the edges and around any
holes and keeps text from directly abutting any other graphics
displayed near the region. You can change the padding from its default
value with the setLineFragmentPadding: method. Note that line fragment
padding isn’t a suitable means for expressing margins; you should set
the NSTextView object’s position and size for document margins or the
paragraph margin attributes for text margins.
Unfortunately, it looks like NSTextField's NSTextContainer and NSLayoutManager are private and inaccessible, but it appears they are accessible in an NSTextView:
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSTextView_Class/Reference/Reference.html#//apple_ref/occ/cl/NSTextView
So that may be the class you need to subclass if you want to have minute control over this kind of functionality.
Have you looked into CoreText? I think it may provide the facilities to do what you're looking for. From the docs...
The Core Text layout engine is designed specifically to make simple text layout operations easy to do and to avoid side effects.
You are able to access "font metrics," which enable you to (from the docs)...
For every font, glyph designers provide a set of measurements, called metrics, which describe the spacing around each glyph in the font. The typesetter uses these metrics to determine glyph placement. Font metrics are parameters such as ascent, descent, leading, cap height, x-height, and so on.
EDIT:
It just may be that NSTextField was not designed for what you are trying to do. NSTextField does custom layout apart from a NSLayoutManager.
You may need to upgrade to a NSTextView, which always has a dedicated NSLayoutManager attached. Apple has some example projects you could search for using NSLayoutManager and NSTextView.
If you're using NSTextField to draw simple static text, take a look at AppKit additions to NSString. Use sizeWithAttributes: to get size of the "text" image. Then use the size to calculate rects for drawing. Finally use one of draw methods to actually draw text. Don't forget to "round" result of sizeWithAttributes! It's not pixel aligned.
But if you need to draw something more complex than simple label, use Core Text. You can find very good example of how to use it in twui source code.

How do I make a UITextView Wrap to accommodate text within it?

I have several UITextViews that contain numbers between one and three digits. I'd like these views to expand and shrink as the number becomes larger or smaller. At the moment the text is shrinking and the TextViews size remains the same.
I'd preferable like to do this using the xib file (I'm very surprised this isn't a meagre check box) but a coded answer would be great if this isn't possible.
I'd recommend using the contentSize property of the UITextView:
self.textView.frame = CGRectMake(self.textView.frame.origin.x, self.textView.frame.origin.y, self.textView.contentSize.width, self.textView.contentSize.height);

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.

Getting Justified Text in UITextField

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.