iOS 7 BUG - NSAttributedString does not appear

后端 未结 7 785
南旧
南旧 2020-12-03 07:04

Last week I asked a question about a Simulator bug with NSAttributedString not displaying: iOS 7 Simulator Bug - NSAttributedString does not appear

Unfortunately it

相关标签:
7条回答
  • 2020-12-03 07:23

    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];
    
    0 讨论(0)
  • 2020-12-03 07:25

    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!

    0 讨论(0)
  • 2020-12-03 07:29

    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.

    0 讨论(0)
  • 2020-12-03 07:34

    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.

    0 讨论(0)
  • 2020-12-03 07:34

    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.

    0 讨论(0)
  • 2020-12-03 07:41

    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.

    0 讨论(0)
提交回复
热议问题