UITextView top of text on first line clipped cut - ios

I have a UITextView with a strange issue. When I type in emojis, the top of the emojis are clipped on the first line. It's not a problem with the scroll and I have tried putting the content inset so the text is deep inside the text view frame, but the problem persists.
Any ideas why this is happening?
THERE IS NO CODE FOR THIS. IT IS SIMPLY A UITEXTVIEW IN INTERFACE BUILDER.
NO CONNECTING IBOUTLETS OR ANYTHING. ATTACHED IS THE INSPECTOR IMAGE

I had this problem when using a custom font. I was able to solve it by setting the LineHeightMultiple in the ParagraphStyle to 1.2, but you'll probably need to play around with different values depending on your font.

Set top edge inset in UItextview this might help you
UIEdgeInsets UIEdgeInsetsMake ( CGFloat top, CGFloat left,
CGFloat bottom, CGFloat right );
[YOUR_TEXTVIEW setTextContainerInset:UIEdgeInsetsMake(10, 0, 0, 0)];

I had the same problem, and found that this problem happen with some custom fonts. For example I was using "Myriad Pro" and the emojis were cut, I just changed it to a different custom font (Museo Sans) and the problem was solved.

Related

Wrapping text in UITextView in Swift

UPDATE: Looks like the UITextView's width is bigger than screen's width.
It is strange since I created the UITextView using a storyboard and in preview mode it looks like it's width is less than that of the UIViewController.
I have a UITextView, in a UIViewController, that was created using storyboard.
I have similar code to populate the text in the UITextView.
self.taskGroupDescriptionTextView.text = taskGroup.description
self.taskGroupDescriptionTextView.textContainer.lineBreakMode = NSLineBreakMode.ByCharWrapping
I tried different types of lineBreakMode, break by char/word. None of them work.
My UIViewController when run on iOS simulator still has text that extends beyond the screen.
You should set constraints. Here's an example how to do it:
You click on your UITextView so that you can start editing it. At the bottom of the storyboard there are few icons and clicking on the one shown in the image will open up an editor where you can add constraints. You have plenty of options (pin to left, right, top, bottom, fix height or width, aspect ratio,... and even more if you click on a neighbour icon). Keep in mind that you have to set enough constraint so that it will be reasonable for your view controller to calculate the frame size of your view. But no worries, if you'll forget something you'll get an error.

Remove UITextView extra border

I've made a UITextView yet there's some unwanted spacing:
(I'm drawing the white box separately). I'm fairly certain I got the coordinates right since if I scroll it gets cut off at the right places:
How can I make the text be drawn further to the left, and also to have the top-most position be further on the top? That is, I'd like it to look like this:
I've tried [[textView layer] setBorderWidth:0.0f], but to no avail - it already doesn't have a border. This is what it looks like if I set the border width to 2 and to the color red:
I'm curious where the extra spacing is coming from and how to control it.
Have you tried experimenting with textContainerInset?
According to the docs at developer.apple.com, "This property provides text margins for text laid out in the text view." This is new in iOS 7.
It seems that the following does the trick:
UITextView *textView = ...;
[textView setContentInset:UIEdgeInsetsMake(-10, -8, 0, 0)];
The result is exactly as I wanted it to be. This isn't too satisfying as it uses magic numbers, so if anyone has a better answer, feel free to comment or answer.

UILabel with NSAttributedString is clipping content

I've got a UILabel set up with auto layout in such a way that its height is based on its intrinsic content size, such that it gets taller when there are more lines in it. I need it to be centered alongside other elements in the same view. With everything default, it works just fine.
However, I'm using a custom font that has a bit too much space in it. I've set up an NSMutableParagraphStyle, like so:
NSMutableParagraphStyle *headlineParagraphStyle = [NSMutableParagraphStyle new];
headlineParagraphStyle.lineSpacing = 0.0f;
headlineParagraphStyle.maximumLineHeight = 20.0f;
headlineParagraphStyle.hyphenationFactor = 0.0f;
headlineParagraphStyle.alignment = NSTextAlignmentLeft;
I'm then creating and setting an NSAttributedString as the UILabel's -attributedText:
NSString *uppercaseHeadline = self.currentStory.headline.uppercaseString;
NSAttributedString *attributedHeadline = [[NSAttributedString alloc] initWithString:uppercaseHeadline attributes:#{NSParagraphStyleAttributeName: headlineParagraphStyle}];
self.headlineLabel.attributedText = attributedHeadline;
The result is that the text looks okay, but it's shoved up above the top of the UILabel and clipped off at the top, while there's still extra space at the bottom of the label:
This also throws off the centering of other items against the text in this label, since you can see that the space between the two lines does not line up with the center of the label's frame.
How can I tell UILabel to recenter this text, so that the top doesn't clip and there isn't any space at the bottom?
I've realized I never came back and answered this question after iOS 7 was released and the NDA on that platform lifted. As of iOS SDK 7.0, it is possible to use the NSAttributedString attribute NSBaselineOffsetAttributeName, which did exactly what I needed to do.
It was available but "no longer supported" in iOS 6. However, when building with the iOS 7 SDK, it appeared to do the right thing.
Edit: In case it's unclear, I don't recommend doing this. If Apple says it's no longer supported, it's probably not a good idea to rely on it. It worked for me, but it's definitely not a long-term solution.
I just had a wild ride with this. For whatever reason, using lineHeightMultiple, maximumLineHeight, and/or minimumLineHeight blows the offset like this.
However, using lineSpacing (which is not an absolute value but a relative value) will change the spacing between lines without messing up the offset.
In iOS 10, my solution was to only set maximumLineHeight and to NOT set minimumLineHeight (otherwise, the top of the label gets clipped). Changing lineSpacing or lineHeightMultiple did not help.
I feel it's important to link to the 'bizarre interview': http://i.imgur.com/pFeqPHd.gif.
You may need to edit the font's ascender property, see here: UIButton custom font vertical alignment
in my case numberOfLines = 0 was needed
otherwise, counterintuitively, line after the newline were clipped

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

truncated italic text in UIButton?

I found a strange behaviour when dealing with UIButton with big font size :
My iPad application need to present those kind of UIButton and I found that when I apply italic property on those big font sized UIButton, the text looks truncated like below :
this is strange as my UIButton is correctly centered and big enough.
sizeToFit doesn't help.
I tried to put log to know more, and it appears that the inside size of the UILabel of the button is too tiny :
NSLog(#"Button width : %.1f, text width : %.1f", button.frame.size.width, button.titleLabel.frame.size.width);
[button.titleLabel sizeToFit];
NSLog(#"Button width : %.1f, text width : %.1f", button.frame.size.width, button.titleLabel.frame.size.width);
which gives me :
Button width : 710.3, text width : 518.0
Button width : 710.3, text width : 518.0
it is doing the same think directly in IB, when applying more than 150 px font size on an italic styled UIButton.
is there a way to fix it or is it an SDK bug ?
You can try out setting the title of the button like the following
[button setTitle:#"2 " forState:UIControlStateNormal/Highlighted/Selected];
or do the same using the .xib file associated with your ViewController.
After that, you have to set the edge insets of the title of the button, which can be done from the corresponding .xib file, or do the following:
[button setTitleEdgeInsets:UIEdgeInsetsMake(0,spacingFromLeft, 0, 0)];
The second step will ensure that the text, in this case, the string "2" remains aligned towards the center. Thus, in the leftSpacing parameter of the method UIEdgeInsetsMake(), you can set the width according to your requirements to get it aligned to the center.
I was also having the same problem and adopted this solution, which may not be the best or correct approach, but it certainly worked for me.
I think its a combination of the large font size (relative to the button/label size). When you add a space, its actually increasing the size of the UILabel.
Try [button.titleLabel adjustsFontSizeToFitWidth]. This will definitely reduce the font size (if its too large for the given frame size), but will make sure that nothing from the character gets chopped off.

Resources