Can't set image as the background of UILabel - objective-c

I have seen lots of questions in stackoverflow with the same issue, but none of the solutions helped me. I have used the following code to set a custom image as the background of a label:
labelBackgroundImageOriginal = [UIImage imageNamed:#"paper.jpg"];
labelBackgroundImageSize = myLabel.frame.size;
UIGraphicsBeginImageContext(labelBackgroundImageSize);
[labelBackgroundImageOriginal drawInRect:CGRectMake(0, 0, labelBackgroundImageSize.width, labelBackgroundImageSize.height)];
labelBackgroundImageNew = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
myLabel.backgroundColor = [UIColor colorWithPatternImage:labelBackgroundImageNew];
But the label background was still white and the image didn't show up. Then I tried with the various solutions that were given under different questions like the following one by one:
myLabel.backgroundColor = [UIColor clearColor];
myLabel.opaque = NO;
But none of them seem to be working as well. Is there actually a way out of this?

Have you try using [UIColor colorWithPatternImage:] directly with your image ? It works fine for me.
UILabel *test = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 100)];
test.text = #"TEST";
test.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"anyPicture"]];
[self.view addSubview:test];
EDIT : I've tried your code and it works fine ! Have you checked if myLabel.frame.size value is not null ?

Related

Changing the size of the UIImage

I have the following code
else if([annotation.title isEqualToString:NSLocalizedString(#"TITLE 1",nil)]|| [annotation.title isEqualToString:NSLocalizedString(#"TITLE 2",nil)])
{
static NSString *identifier = #"currentLocation";
SVPulsingAnnotationView *pulsingView = (SVPulsingAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if(pulsingView == nil) {
pulsingView = [[SVPulsingAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
pulsingView.annotationColor = [UIColor colorWithRed:0 green:0.678431 blue:0 alpha:1];
pulsingView.image = [UIImage imageNamed:#"image.png"];
pulsingView.frame = CGRectMake(0, 0, 15, 15);
pulsingView.canShowCallout = YES;
}
return pulsingView;
Basically it's an image and besides it we have a pulsing round annotation.
When the image is tapped - the annotation description with either "Title 1" or "Title 2" comes up depending on the conditions (which are not relevant here).
The issue is simple - when i apply
pulsingView.frame = CGRectMake(0, 0, 15, 15);
The image on top of the pulsing view doesn't change size.
Need help with alternative solutions.
Thank you so much guys!
1) Try this
[pulsingView setFrame: CGRectMake(0, 0, 15, 15)];
instead of
pulsingView.frame = CGRectMake(0, 0, 15, 15);
2) If the first solution doesn't work maybe you need to disable the autolayout if you are using Storyboard or...
3) if you use XIB maybe you need to add
[pulsingView setNeedsDisplay];
If previous answer doesn't work, a little trick that I've used sometimes is calling setTranslatesAutoresizingMaskIntoConstraints:.
Your code would look like:
[pulsingView setTranslatesAutoresizingMaskIntoConstraints:YES];

UITextView renders custom font incorrectly in iOS 7

My app uses a UITextView to input Syriac text (Estrangelo font), but UITextView renders some letters incorrectly like this:
I tested it with a UILabel and a UITextView. UILabel displays it correctly, but UITextView incorrectly displays the top dots and moves them to the bottom (see the above result).
This problem only occurs in iOS 7 and does not occur in iOS 6. Please tell me if there's any way to fix the problem.
This is my test code
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
label.center = CGPointMake(self.view.center.x, self.view.center.y-40);
label.font = [UIFont fontWithName:#"East Syriac Adiabene" size:24];
label.text = #"ܩ̈ ܡ̄ ܬ̇ ܒ̃";
[self.view addSubview:label];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
textView.center = CGPointMake(self.view.center.x, self.view.center.y+40);
textView.font = [UIFont fontWithName:#"East Syriac Adiabene" size:24];
textView.text = #"ܩ̈ ܡ̄ ܬ̇ ܒ̃";
[self.view addSubview:textView];
I debugged this issue a little, and it seems to be a bug in the way NSLayoutManager layouts the text. As other answers pointed out, UITextView is build around TextKit since iOS7, and thus uses NSLayoutManager internally to layout text. UILabel uses Core Text to layout text directly. Both eventually use Core Text to render the glyphs.
You should open a bug report with Apple and post the number so people can duplicate it. The issue has not been fixed so far in iOS7.1 betas.
As a workaround, you can replace UITextView with other Core Text alternative editors, which layout and render directly with Core Text, where the issue does not exist.
I tested SECoreTextView, and it shows the text correctly. It implements a similar API to UITextView but internally uses Core Text.
Here is how it looks after swapping UITextView with SECoreTextView:
SETextView *textView = [[SETextView alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
textView.center = CGPointMake(self.view.center.x, self.view.center.y+40);
textView.font = [UIFont fontWithDescriptor:desc size:24];
textView.text = #"ܩ̈ ܡ̄ ܬ̇ ܒ̃";
textView.textColor = [UIColor blackColor];
textView.backgroundColor = [UIColor whiteColor];
textView.editable = YES;
[self.view addSubview:textView];
Some days ago, I had the same problem as yours in iOS 7 (but the font was different).
I set the FONT after setting the TEXT and it worked for me. So for your case:
textView.text = #"ܩ̈ ܡ̄ ܬ̇ ܒ̃"; // Setting the text.
textView.font = [UIFont fontWithName:#"East Syriac Adiabene" size:24]; // Setting the font and it's size.
It may seem silly, but it worked for me.
Also see this question/answer. There are many tips for using custom fonts with UITextView, which may be helpful for you.
EDIT :
iOS 7 also introduced a new selectable property on the UITextView for enabling text selection. So make sure you have done the following:
self.textField.editable = YES;
self.textField.selectable = ON;

Fixed UIImage PushViewController animated

I want to get an image fixed between the animation, so it doesn't move when the app slides to right. A bit like the background of a UINavBar, I couldn't find anyone who tried it earlier so I would really like to know.
Thanks in advance
Sjors
As a response on your last comment, here is how to do this:
UIView * hoi = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
hoi.backgroundColor = [UIColor blackColor];
UIWindow * window = [UIApplication sharedApplication].keyWindow;
[window addSubview:hoi];
Alternatively you create add a second UIWindow to which you can add your subviews. As is nicely explained in this answer: https://stackoverflow.com/a/2671148/262691

AQGridViewCell transparent background

I have a problem customizing my AQGridViewCell. I'd like to have the whole cell having a transparent background, but the following inside of the initWithFrame:reuseIdentifier does not do the job:
self.backgroundView.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor clearColor];
self.backgroundColor = [UIColor clearColor];
self.backgroundView.opaque = NO;
self.contentView.opaque = NO;
self.opaque = NO;
Does anyone have an idea how to solve this?
Thank you very much for any response!
EDIT
I found this, but this does not seem to work either:
https://github.com/AlanQuatermain/AQGridView/pull/108#issuecomment-3610006
The tip in your link was half way there. The following did the trick for me:
self.contentView.backgroundColor = nil;
self.backgroundColor = nil;
And you need to put this in your custom AQGridViewCell's initWithFrame:reuseIdentifier:. It's a bit puzzling that you have to set two properties but at least it works.
Also note that you also need to set the background color to clear for all text labels you might have, e.g:
captionLabel.backgroundColor = [UIColor clearColor];
Setting label background to nil doesn't help - it comes out as black.

UILabel sizeWithFont: problem. Clipping italic text

I have created a UILabel that displays a single large character. Even with clipsToBounds = NO; I still get clipping.
See link: http://img341.imageshack.us/img341/5310/screenshot20100814at243.png
I used the following code:
CGSize fBounds = [myLabel.text sizeWithFont:cFont];
To get what should be the bounding rectangle of the font. And the label is drawn with:
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 300)];
myLabel.clipsToBounds = NO;
myLabel.numberOfLines = 1;
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.adjustsFontSizeToFitWidth = YES;
myLabel.minimumFontSize = 10;
myLabel.text = #"A";
myLabel.font = [UIFont fontWithName:#"CourierNewPSMT" size:300];
myLabel.textColor = [UIColor blackColor];
myLabel.backgroundColor = [UIColor colorWithRed:1 green:.5 blue:0 alpha:.5];
In the image below, the size returned from sizeWithFont is rendered by the semi-transparent blue rectangle overlay. As you can see, with an italic font (in this case Verdana-BoldItalic), the character extends past what sizeWithFont returns. Further, the UILabel's frame (the orange color) also clips the character. Thoughts? Maybe I could override some text drawing routine. Also, not sure if this is the same problem as here:
UIButton.titleLabel clipping text problem
Use attributed text + indent...
Looks like this is an apple problem. Ended up doing custom drawing with CoreText.