1

I have a UILabel and a UITextView both with the same text (the string "SnellRoundhand", in Snell Roundhand Bold font, point size 21). The text in the UITextView appears correctly, but the UILabel has its text cut off on the left and right sides. How do I get the text in the label to appear properly?

https://twitter.com/dandelarosa64/status/307549623717867520/photo/1

Some notes:

  • Expanding the frame of the label won't work because it might solve the cutoff issue on the right side of the text but not on the left side.
  • I can't take the cheap way out and center the text; the text must stay at whatever alignment it is in right now.
  • The reason I can't just change everything to UITextViews is because my app does some processing in the background and it crashes whenever it instantiates a UITextView. I'm hoping I can get around the issue by using UILabel instead to render the text.
d-squared
  • 107
  • 2
  • 9
  • Have you tried the `adjustsFontSizeToFitWidth` property? – trojanfoe Mar 01 '13 at 15:54
  • Setting the `adjustsFontSizeToFitWidth` property does not change the appearance of the label. Also, I need the text's font size to remain the same. – d-squared Mar 01 '13 at 16:04
  • If you're crashing when you instantiate a text view, that suggests that you're doing it somewhere other than the main thread. Most UIKit objects must be managed only on the main thread. – Rob Napier Mar 01 '13 at 17:41
  • Update: according to this page (http://www.cocoanetics.com/2012/12/radar-uitextview-ignores-font-kerning/) UITextView uses WebKit which would explain why the text appears differently here than on a UILabel. This also explains why the UITextView crashes when created on a background thread (and UILabel doesn't). – d-squared Mar 01 '13 at 23:09

3 Answers3

3

In iOS 6, a very nice new feature of UILabel is that it supports attributed strings. Attributed strings can include paragraph margins. So you can add margins to your string, thus ensuring that there will be some extra space between the edges of the label and the drawing of the string.

This section of my book has sample code that adds margins to a string drawn in a UILabel:

http://www.apeth.com/iOSBook/ch23.html#_attributed_strings

If you want to run on a system earlier than iOS 6, you can subclass UILabel to force the text to be drawn inset from the edges of the label, as shown in this code (also from my book):

- (void)drawTextInRect:(CGRect)rect {
    [super drawTextInRect:CGRectInset(rect, 5.0, 5.0)];
}
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Looks promising. I'll need to try it out with several test cases before passing judgement on this. – d-squared Mar 01 '13 at 18:46
  • @matt you appear to have a great understanding of TextViews. I am also struggling with an NSAttributedString/UITextView Issue: http://stackoverflow.com/questions/15169064/how-to-consistently-draw-nsattributedstring-in-uitextview-in-uitableviewcell – Piotr Tomasik Mar 02 '13 at 00:36
0

I don't have a good answer, but I can suggest a kludge that sorta does what you want done and might give you some useful ideas. The problem sure seems to suggest a fundamental problem with Label and TextView handling for funky fonts.

The concept is simple enough:

  • Size the TextField with left (or right) justification to just fit the contents.
  • Slightly enlarge the text field width.
  • Change the justification to center.

This will result in a field just wide enough to display the text without clipping (if you enlarge it the right amount). I know you said you couldn't change the text alignment, but doing it this way only moves the text a point or two and it ends up where it needs to be to display the full text. The field ends up the size, and the text in the position it ought to be. For instance:

self.textField.textAlignment = NSTextAlignmentLeft;
[self.textField sizeToFit];
CGRect frame = self.textField.frame;
frame.size.width += 4;
self.textField.frame = frame;
self.textField.textAlignment = NSTextAlignmentCenter;

This works. If it isn't useful to you directly I hope it gives you some ideas.

Something I tried that wasn't helpful was subclassing UITextField and overriding the textRectForBounds: to enlarge the area used to draw the text. I could move the text starting position slightly to the right, but it still clipped the left edge. Turning off the clipsSubviews property didn't help. Seems like Apple's problem here.

Charlie Price
  • 1,266
  • 11
  • 20
  • There's something I didn't realize about the problem I posted: my app needs to support multiline text which UITextField doesn't support. So I can't use this response. Sorry about that. I'm editing the question so it doesn't mention UITextField anymore. – d-squared Mar 01 '13 at 17:58
  • For what it is worth, the same kludge works with UILabel -- except not with multiple lines of course. – Charlie Price Mar 01 '13 at 18:10
  • Yeah, the kludge doesn't work when the label has multiple lines, it centers every line after the first one. – d-squared Mar 01 '13 at 18:24
0

On iOS 6 the UITextView has a margin on each side, you can adjust the content inset to prevent the text from using those margins.

[textView setContentInset:UIEdgeInsetsMake(-8, -8, -8, -8)];

pablobart
  • 2,641
  • 1
  • 24
  • 22
  • Sorry for the delayed response, but this doesn't solve the problem. I'm looking for a way to get the UILabel to look like the UITextView, not the other way around. – d-squared Oct 08 '13 at 22:11