Last week I asked a question about a Simulator bug with NSAttributedString not displaying: iOS 7 Simulator Bug - NSAttributedString does not appear
Unfortunately it now appears this is not a simulator bug but an iOS 7 bug. I have now reproduced this issue on an iPhone 5 device.
The bug appears to be the combination of using NSUnderlineStyleAttributeName & NSParagraphStyleAttributeName as attributes for a NSAttributedString.
I have only tested on two iOS 7 devices so far, and the issue has only appeared on one of them. Even after they have both been upgraded to the exact same version:
1st iPhone 5 with iOS 7.0 (11A465): Text does NOT appear
1st iPhone 5 after upgrading to 7.0.2 (11A501): Text does NOT appear
2nd iPhone 5 running iOS 7.0 (11A4449d): Text displays correctly
2nd iPhone 5 after upgrading to 7.0.2 (11A501): Text does NOT appear
So it appears Apple introduced this bug after iOS 7.0 (11A4449d). I've filed a bug with them and will update you on any response I get.
Steps to reproduce bug
If you are running iOS 7.0.2 then you should be able to reproduce this bug.
Either download and run this project on your device https://github.com/rohinnz/iOS-7-BUG---NSAttributedString-does-not-appear
or
1) In Xcode 5 create a new 'Single View Application'. Call it whatever.
2) In ViewController.m, replace the viewDidLoad method with:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableParagraphStyle* paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentCenter;
NSAttributedString* attrStr = [[NSAttributedString alloc] initWithString:#"Lorem ipsum dolor sit" attributes:
#{NSUnderlineStyleAttributeName:#(NSUnderlineStyleSingle),
NSParagraphStyleAttributeName:paragraph}];
UILabel* myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 0, 0)];
myLabel.backgroundColor = [UIColor greenColor];
myLabel.attributedText = attrStr;
[myLabel sizeToFit];
[self.view addSubview:myLabel];
}
3) Compile and run on your device. Depending on your version of iOS 7, the text will either display, or will not. The UILabel's background color will display in both cases.
Screenshots
iPhone 5 with iOS 7.0 (11A465)
iPhone 5 with iOS 7.0 (11A4449d)
My Question
Is anyone able to reproduce this issue on a device?
I have found a workaround for this bug. In your github code, to use the workaround, you would say this:
NSAttributedString* attrStr =
[[NSAttributedString alloc] initWithString:#"Lorem ipsum dolor sit\n" // <---
attributes:
#{NSUnderlineStyleAttributeName:#(NSUnderlineStyleSingle),
NSParagraphStyleAttributeName:paragraph}];
UILabel* myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 0, 0)];
myLabel.backgroundColor = [UIColor greenColor];
myLabel.attributedText = attrStr;
[myLabel sizeToFit];
myLabel.numberOfLines = 2; // <---
I have made two changes: I've appended a newline character to your original string, and I've set the label's numberOfLines to 2.
What the workaround does is to force the label's text up against the top of the label. This seems to solve the problem. In other words, the bug appears to stem from the label's attempt to vertically center its text; if you deliberately make the text too long for the size of the label by juggling the label's height, numberOfLines, and excess newline characters at the end of the text, the attributed string will be drawn.
EDIT I've just found another workaround along the same lines: let the label resize itself to fit the text. See my code and explanation here: https://stackoverflow.com/a/19545193/341994 In that code, I do the same thing from the opposite end, as it were: I give the label a fixed width constraint but a flexible height constraint, and by setting its own height, the label brings the top of its text up against the top of itself, and thus is able to display the text correctly. In other words, this is just another way of preventing the label from centering its text vertically, which is what seems to trigger the bug.
FURTHER EDIT I have the sense that this bug may get fixed in iOS 7.1.
I also had the same problem when setting the background color on text of a UILabel in a UITableViewCell. My workaround was to use a UITextView with UserInteraction disabled instead of a UILabel in the cell and it worked.
Update: Found the issue only appearing with UILabel included in Basic UITableViewCell.
Update 2: Also found that the problem does not occur when a UILabel wraps to multiple lines of text. One workaround is to force text to wrap by adding a newline and space. Very hacky, but it works. Make sure numberOfLines is set to zero and lineBreakMode is set to NSLineBreakByWordWrapping.
I had the same issue in my application. It was occurring in the simulators, as well as on my device (iPhone 5 running 7.0.2 (11A501)). I then realized that my UILabels living in other ViewControllers were displaying NSAttributedStrings using the NSUnderlineStyleSingle attribute properly.
After some troubleshooting. It seems that if you're using the default font (I'm using System 17.0) and your UILabel has a height of less than 62 pixels, it will display correctly regardless of what background color, text color, or alignment you are using. A change of the UILabel's height to a value greater than 61 pixels, allowing auto-sizing to change the height for you, or a change of the font to a Custom one will result in the disappearing of the underlined NSAttributedText.
At first I thought this issue might be due to my positioning the UILabel behind the new Status Bar (or lack thereof), but even in positions which would interact with this new feature, the height-rule still held. I find it hard to believe that the height of the UILabel would cause such an issue, but that's the conclusion I came to.
Wow, took me a while to find this. Looks like I'm have a similar problem as Indi. Setting the background color of an attributed string caused the text to just disappear. Only place I can reproduce this is on a device running iOS 7.0.3.
workaround: use an image view
CGRect rect = self.frame;
CGRect rr = [attribText boundingRectWithSize:rect.size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesDeviceMetrics context:nil];
UIGraphicsBeginImageContextWithOptions(rr.size, NO, 0.);
[attribText drawWithRect:rr options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesDeviceMetrics context:nil];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imView = [[UIImageView alloc]initWithImage:image];
[self addSubview:imView];
I noticed this problem occurring in a similar way but ended up with a different solution. The string would sometimes disappear, while the solution proposed above helped ensure the text did not disappear, the text would often show up without any of the attributes I had set (strikethrough, different colours, etc.)
Here's the setup:
A legacy project using springs and struts being built using Xcode 6.1.1 and iOS SDK 8.1. The problem was more noticeable on iPad devices as compared to iPhone devices (~ 5% of the time on iPhones vs. 95% on iPads). Regardless whether I used numberOfLines, sizeToFit or other methods, the attributes would not show up correctly on an iPad or iPhone 100% of the time.
The solution was to switch to Auto Layout and employ the solution above (numberOfLines = 2, sizeToFit seemed optional for my situation)
It seems there's a bug with Attributed Text on UILabels with Springs and Struts when they get stretched horizontally.
Hope this helps someone!
i had this problem also and it seams to manifest only on specific languages and on iOS 7.0 , i had this issue when i want to underline the text in chinese, solved the problem with [ label sizeToFit] hope it will help ;)
Constantin.
Related
I am setting a custom height and width to the UIPickerView with
mypicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, myWidth, myHeight)];
While it works perfectly on simulator ios 10, it ignores the custom height and width on ios 8. Is there a way do this in ios 8?
I tried CGAffineTransform and it didn't work.
Many thanks in advance!
The reason this doesn't work on iOS 8 is that the ability to provide your own height was not introduced until iOS 9. In iOS 8, the picker view will resist any attempts at changing its height; there is a narrow legal range of heights, and any attempt to set the height outside that range will just fail (and to add to the misery, the legal range is undocumented).
There are only three valid heights for UIPickerView (162.0, 180.0 and 216.0).
You can use the CGAffineTransformMakeTranslation and CGAffineTransformMakeScale functions to properly fit the picker to your convenience.
For more information this link and this link may help .
Happy to help!
When setting the tint color of a UISegmentedControl using the Appearance API, the color of the text in each unselected segment takes the color of the UILabel instead only after switching tabs.
A sample program to test this (screenshots below):
Load the program and look at the first tab. Everything is normal, the labels are red and the segments are blue.
Switch away to the second tab, everything is still fine.
Switch back to the first tab, you will see that the segments have changed to be red instead of blue like they should.
App was just loaded, everything is fine:
After switching tabs, color is wrong:
Code responsible (in the app delegate for testing, but happens elsewhere):
[[UILabel appearance] setTextColor:[UIColor redColor]];
[[UISegmentedControl appearance] setTintColor:[UIColor blueColor]];
I have sent this information to Apple in a bug report. They asked for a sample project, but I haven't gotten an answer yet. This only shows up on IOS 7.1. On 7.0, this doesn't happen.
Are there any suggestions or temporary fixes that would resolve this? It makes my app look bad even though I don't think it's my fault (the red is just to test, that would make anybody's app look bad). I have tried setting controls manually by setting the tint of the specific control instead of using the appearance API, but the problem is still there.
As discussed in the comments, use [[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setTextColor:[UIColor blueColor]]; to set the appearance of the internal label contained in a segment control.
Per the Apple documentation: Setting the tintColor property by using the appearance proxy APIs is not supported in iOS 7.
iOS 7 UI Transition Guide
You can also specify text attributes like of UISegmentedControl like font, using setTitleTextAttributes:forState.
I have an UISegmentedControl with 100x42 bounds. I've set up three segments with 26x26 images and 4x42 divider images. The three segment images should fit in the segments, but they are scaled and for the worse they seem to fit vertically and are only scaled down horizontally, thus loosing proportions.
This problem appeared after i changed to Xcode 5 and iOS 7 SDK. Before that the segment images were displayed with correct proportions and in original size.
I'm not using interface builder. I've tried to set the segment sizes manually and setting the segmented control's contentMode to UIViewContentModeScaleAspectFill without help.
Is there any way to force the UISegmentedControl to simply render the images as they are?
You may set auto layout constraints to correctly define UISegmentedControl frame. Like this
I know this is a late answer but I have the same issue testing an app for IOS 7.1.
If you set the image in the code it's working properly. Example:
- (void)viewDidLoad {
[super viewDidLoad];
[_segmentedControl setImage:[UIImage imageNamed:#"779-users-toolbar"] forSegmentAtIndex:0];
}
In my project I have the normal image, and the #2x & #3x versions of the image.
I tested the code in 7.1, xcode 6.1.
When I run it in 8.1 the image set via IB works properly as well, so this may be a bug in 7.1 that has been fixed in 8.
I'm using IcoMoon to create a custom font that I use to generate Icons. This however plays no role as the same phenomenon occurs using Helvetica.
Why do 2 things occur on titleLabel of a UIButton on iOS 7?
The right edge of the label is cut off (the button frame is much wider, but the label frame is getting cut off
there is a dark ring around the light gray image.
This looks absolutely horrible and it's basically unacceptable. We'll have to render graphics ourselves and localize images where appropriate.
I would be grateful for any info. The only thing that distinguishes these two checkmarks is that they have different colors set for titleColor
Try setBackgroundImage: instead of setImage:
My app shows up letter boxed and I want it to stretch properly to fill the whole screen. I'm creating my main window with:
self.mainWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
But I'm still getting the letter boxed behavior. Is there some other setting I need to set to get the full size of the mainScreen on the device?
I'm trying to avoid using any launch images if I can. I've tried this:
self.mainWindow.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
and it doesn't seem to help. And I've looked through all the Info.plist options and found nothing that seems to match.
I don't support your idea of avoiding launch images as they are there for a reason, but if you really wanna do it for some reason, you should use the Default-568h#2x.png file (640x1136 px) like the one you get when creating a new project. It's just solid black, doesn't take up a lot of disk space and will look as if there are no launch images whatsoever.
Besides setting a 4-inch retina launch image, there is currently no known way to get full 1136 px height on the new screen.