简体   繁体   中英

How do I prevent text from being cut off like in UITextView?

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.

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)];
}

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.

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)];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM