Strange behaviour of NSAttributedString in iOS 8 - ios

When I checked with iOS 6, I can change text size of a UITextView (not a text field) by adding attributes without any issues. (By changing UITextView's attributedText property)
For eg:
AAAAA
BBBBB
If I have above text in a UITextView, I can increase the size of the text portion of BBBB without it overlapping AAAA in iOS 6. BBBB text would increase it's size to the down side direction. Even if I increase the size of AAAA it would increase it's size towards BBBB but it would pushes BBBB text towards down to get space.
But when I run the same code in iOS 8, whenever I increase the size, BBBB part overlaps AAAA. Because in iOS 8, BBBB would change it's size to the up side direction so it overlaps AAAA text.
Is this a bug in iOS or is there a way to fix this ?

Problem solved. I had set paragraphStyle.maximumLineHeight property to the size of Font size. Removing that line did the trick.

Related

Different font Textsizes for different devices in Xcode 6 [duplicate]

I want to fit my text in a UILabel, but for different iPhone the size of the UILabel is changing, as I'm using auto layout, but I cannot fix the font size, so my text is cutting out.
Is there any way I can set any constraint so that the text fits in the UILabel dynamically?
See here the text got cut, because of different screen resolution
You should use autoshrink.
Since all iPhones have the same Compact width size class when in portrait mode, you can't rely on this to handle your label size.
Previews are for iPhone5, iPhone6 and iPhone 6+
In the inspector, you must select minimum font scale or minimum font size in front of Autoshrink. This enables the content to change the size of the font to fit in the label.
Here, I set minimum font scale to 0,5 so the minimum size is half of the current size (31.0). The text will try to fit until it reaches the minimum scale/size.
(Generally do not use "Tighten letter spacing" for this purpose. Tighten letter spacing uses the same font size and reduces spacing between letters. It can make the label up to 5% tighter before truncating, but it's not effective when minimum font scale/size is enabled.)
You may want to test with a wide screen device such as the iPad Pro, and also on a smaller screen such as the iPhone 4S. As a good practice you should test with different user system font sizes, you can set them in Settings>General>Accessibility>Larger Text.
Autoshrink will not adjust the font size bigger than the one set on the label, that means if you make the label the same width as the screen but leave the font size to 14, it will try to increase the font size until it reaches that size.
To make it actually work, select a big font size.
You can still combine autoshrink with size classes to change the maximum font size depending on the device/the orientation.
In case you want to use autoshrink with UIButtons, you can still set this behavior with two lines of code.
myButton.titleLabel.minimumScaleFactor = 0.5;
myButton.titleLabel.adjustsFontSizeToFitWidth = YES;
Hi if you are adding UILabel from storyboard you can set different font for all available layout.
You can do this by using size classes.
each display dimension using a size class, this will result in four abstract devices: Regular width-Regular Height, Regular width-Compact Height, Compact width-Regular Height and Compact width-Compact Height.
The table below shows the iOS devices and their corresponding size classes.

To set the font size for this particular size class, first select the UILabel. Under the Attributes inspector, you should see a plus (+) button next to the font field. Click the + button and select Compact Width > Regular Height (Or as per your requirement select width & Height). ​ You will then see a new entry for the Font option, which is dedicated to that particular size class. Keep the size intact for the original Font option but change the size of wC hR (OR as per your selection) font field to required points (for example 14).

iOS UILabel bounding rectangle not sized correctly

I have a UILabel (as highlighted in yellow), which has the following conditions applied to it.
Label has variable text length
Font set to 40
Minimum font-size set to 20
Number of lines set to 3
Although this looks like a duplicated question I believe it is not. The issue I am having is that when the text exceeds the available 3 line length after being sized-down to 20 points, the UILabel's bounding box is sized incorrectly (i.e. note the extraneous spacing above and below the text).
The end result should be a UILabel without any spacing. Is there are solution to this, while keeping the number of lines set to 3?
That looks like a bug. If you increase your base font size, you will see the space increase. Also, if you inspect the layout at runtime, you will see the content size to be calculated as too big.
My guess is, UILabel takes your original font size (40) to calculate the content size for 3 lines of text and does not take into account that the font size has already been decreased before truncation.
I fiddled with content hugging/compression priorities but could not make it work either.
The only workaround I found was to manually set the font size down to 20. That will get you the frame you want.

2 UIButtons with different text lenght same font size

I have 2 buttons with the same width and height, also y-Achse centered. They differ only from text length. With iPhone 5, 6 and 6+ they look very well. But with iPhone 4s one button will do not more have the same front size as the other one and it is text will be not more centered. My question is if there is a method to adopt the font size of the 2 buttons so they will take together the same font with small value of font size of one button.
If I understand correctly, you want to use the same font size for both buttons, even when one of them has to reduce the size for the text to fit it.
Use something like this to reduce the size of the font on each button, until an appropriate size is found for both buttons. Take the minimum of the sizes you found, and use it on both buttons.

UITextView and the Mysterious Missing Space

I have an app with a handful of UITextViews of various sizes. It appears that if a UITextView small enough has a font.pointSize high enough, there's no way to add a space after the text is big enough to fill the text view.
For example:
I'm trying to figure out what was going on here, I starting typing my usual debug string, I typed "What" hit the space key and no space appeared (but this is usual). I typed "the" and the looked like it was tacked right onto the end of the previous word. Sure enough, there was no space. I could go back and add the space just fine and once I do add the space, I can add other spaces as word wrapping then becomes effective.
Another mysterious behavior is that when you double tap space, it doesn't add a period to the end. It replaces the last character with a period. So, "What" + space + space becomes "Wha."
Now, I'm doing some interesting things with the font size like I'm automatically resizing the font so that the text fills the space provided within reasonable bounds, but when I disable that, I can still reproduce the behavior. The only difference then is that instead of fitting on one line, the word wraps to the next line.
For example, if I type "What" + space + "the" it comes out "Whatthe" with "What" on the first line and "the" on the second (though I can only see the tops of the "the." Further, here's some log information from the textViewDidChange:.
Character textView.text.length
--------- --------------------
W 1
h 2
a 3
t 4
<space> 4
t 5
h 6
<space> 7 <---- Here's a wierd one . . . now spaces all
? 8 work fine unless it's resizing
Another mysterious behavior is that when you double tap space, it
doesn't add a period to the end. It replaces the last character with a
period. So, "What" + space + space becomes "Wha."
That is not mysterious. That means that your UITextView uses autocorrection.
Fix:
UITextView *txtView; // your UITextView
[txtView setAutocorrectionType:UITextAutocorrectionTypeNo];
About spaces... as space is not a drawable symbol, your UITextView can't measure is size (width differs with different alignment), so your UITextView's contentSize property won't be changed and behaves mysteriously. You should set contentSize manually (you can easily calculate size of your NSString with sizeWithFont: method).

Changing automatic font size in UILabel inside a View

I'm drawing a bingo card (UIView) with 27 squares: 9 columns x 3 lines. Cards can have 3 different sizes, so they must be resizable. And inside the card, every square with a number is an UIView with a custom class. Inside the view (in the .xib file) there is the UILabel, where the number is set with:
[lblNumber setText: [NSString stringWithFormat:((number < 10) ? #" %i" : #"%i"), number]];
When numbers have two digits (10->90) everything looks great inside the squares, but for numbers from 1 to 9, label displays with a bigger font size than the others because it only has 1 digit.
Ok, I tried to uncheck the "Adjust to fit" setting, but then font size remains small.
Autosizing settings of the label are set to both arrows, so the label has "space" to resize.
¿How can I have single digit numbers to resize the exact proportion than numbers with two digits?
As you can see in the line of code, I also tried to concatenate a space for numbers with one digit.
Thanks in advance.

Resources