IOS8 UILabel BUG? Text is not displayed, ios8uilabel
In a previous project, we found that there is a place on iOS8 that does not display text, and UILabel is used.
So I debugged the code and roughly found the cause:
When you add a subView that is also a UILabel to a UILabel, if you set the background color (setBackgroundColor :) For the parent Label but do not set the Text (setText :), the text set by the sub-Label cannot be displayed, which is a strange problem.
There are three solutions:
1. Change the parent Label to UIView.
2. Do not set the background color of the parent Lable.
3. Set the Text of the parent Label ([superLabel setText: @ ""])
It is unclear whether this is an iOS8 BUG.
Objective-c makes an ipad program to change the text attribute of UILabel without changing the text
Lable. text = @"";
Change
Self. lable. text = @"";
Try
Enable UILabel to automatically adjust the size based on the font size
In most cases, when binding dynamic data to UILabel, you often need to dynamically adjust the width or height of UILabel based on the number of strings. The following two considerations: 1. The UILabel width remains unchanged. The height of the UILabel is automatically adjusted based on the font size and displayed in a line. The Code is as follows: UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (0, 10,200, 20)]; label. font = [UIFont boldSystemFontOfSize: 20366f]; // The font size label of UILabel. numberOfLines = 0; // This attribute must be defined; otherwise, UILabel will not wrap the label. textColor = [UIColor whiteColor]; label. textAlignment = NSTextAlignmentLeft; // text alignment mode [label setBackgroundColor: [UIColor redColor]; // The width remains unchanged, calculate the label height based on the number of words NSString * str = @ "you can change this content for testing. The width remains unchanged and the height is automatically adjusted according to the content"; CGSize size = [str sizeWithFont: label. font constrainedToSize: CGSizeMake (label. frame. size. width, MAXFLOAT) lineBreakMode: NSLineBreakByWordWrapping]; // reset the UILabel size based on the calculation results [label setFrame: CGRectMake (0, 10,200, size. height)]; label. text = str; [self. view addSubview: label]; [label release]; 2. The height of UILabel remains unchanged. The width of UILabel is automatically adjusted based on the font size and displayed in lines.
The Code is as follows: UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (0, 10, 20, 20)]; label. font = [UIFont boldSystemFontOfSize: 20366f]; // The font size label of UILabel. numberOfLines = 0; // This attribute must be defined; otherwise, UILabel will not wrap the label. textColor = [UIColor whiteColor]; label. textAlignment = NSTextAlignmentLeft; // text alignment mode [label setBackgroundColor: [UIColor redColor]; // rows with fixed height without compromise, calculate the width of the label according to the number of words NSString * str = @ "the width is not changed. Obtain the length required for displaying a single line without a string"; CGSize size = [str sizeWithFont: label. font constrainedToSize: CGSizeMake (MAXFLOAT, label. frame. size. height)]; NSLog (@ "size. width = % f, size. h ...... remaining full text>