UITextField – opacity of left placeholder - ios7

I have a UITextField with opacity 0.2 (the background is red), and I tried to add a white icon as textField.leftView like so:
self.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"lock.png"]];
self.leftView.layer.opacity = 1.0;
self.layer.opacity = 0.2;
self.leftViewMode = UITextFieldViewModeAlways;
The problem is that the leftView seems to get the same opacity as the whole text field, and as they both are white, the result is that I see nothing. Do you see any other way/what I do wrong? Any help is appreciated.

Okay, found the solution. Seems I didn't think it fully through.
The problem was the opacity being "inherited" by all colors of the view, while I only wanted the background to be opaque. So, I just made the background opaque :) Like so:
self.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.2];

Related

Why does border thickness change with color of button (blue gets 2px border)?

I'm working on a game, and for some reason the border thickness changes depending on what color the button is. This problem didn't happen until I put a background image on the app, so I'm not sure what's causing it. I took a couple screenshots to show what I mean. For some reason, the border thickness changes to 2px instead of 1px when the button that is being bordered is blue.
In addition, there are some problems with the borders on the control buttons. By control buttons, I mean the 6 buttons up top that are in the order of Red, Blue, Brown, Green, Purple, and Yellow.
Here are the screenshots:
I create the borders by putting a button in the background which starts at x-1,y-1 and has a width of width+2 and height of height+2. Here is a piece of code to give you an idea:
UIButton *borderButton = [UIButton buttonWithType:UIButtonTypeCustom];
borderButton.frame = CGRectMake((controlX-1), (controlY-1), 42, 32);
borderButton.backgroundColor = [UIColor blackColor];
[self.view addSubview:borderButton];
UIButton *controlButton = [UIButton buttonWithType:UIButtonTypeCustom];
[controlButton addTarget:self
action:#selector(doMove:)
forControlEvents:UIControlEventTouchDown];
controlButton.frame = CGRectMake(controlX, controlY, 40, 30);
controlButton.tag = 500 + i;
tmpColor = [self.colors objectAtIndex:i];
controlButton.backgroundColor = tmpColor;
[self.view addSubview:controlButton];
This code is taken from a loop, where controlX and controlY give x/y values for where the control buttons should start.
If anyone knows how to fix this I would greatly appreciate it. If I am going about borders in the wrong way and there is an easier and less problematic way of achieving the same look, I would be willing to do that as well, because there wouldn't be much code to change on my end. Thanks in advance to anyone who can help me solve this irritating UI glitch.
The easiest way to implement custom button with border is to use the button's layer:
#import <QuartzCore/QuartzCore.h>
...
UIButton *controlButton = [UIButton buttonWithType:UIButtonTypeCustom];
[controlButton addTarget:self
action:#selector(doMove:)
forControlEvents:UIControlEventTouchDown];
controlButton.frame = CGRectMake(controlX, controlY, 40, 30);
controlButton.tag = 500 + i;
// Set layer properties
controlButton.layer.borderWidth = 1.0;
controlButton.layer.borderColor = [UIColor blackColor].CGColor;
controlButton.layer.cornerRadius = 5.0; // if you want rounded corners...
tmpColor = [self.colors objectAtIndex:i];
controlButton.backgroundColor = tmpColor;
[self.view addSubview:controlButton];
You may also need to check that button and other elements (like labels) are located at integral coordinates to avoid blurred images. To correct the coordinates use CGRectIntegral
P.S. For complex animations/game logic it is better to use frameworks like Cocos2D

UIScrollView won't adjust height to fit subviews

I have a UITextView, which sets its text dynamically from an RSS feed. The textview is a subview of a UIScrollview. Ultimately, I am creating a mobile newspaper app, so the user should be able to scroll through the text (and other subviews).
After creating the views in IB, I added
NSString *sourceCode = [NSString stringWithContentsOfURL:[NSURL URLWithString:self.URL] encoding:NSUTF8StringEncoding error:&error];
sourceCode = [self parseHTMLText:sourceCode];
CGSize maximumLabelSize = CGSizeMake(320,9999);
CGSize txtStringSize = [sourceCode sizeWithFont:self.body.font
constrainedToSize:maximumLabelSize];
CGRect newlblFrame = CGRectMake(0, 0, 320, txtStringSize.height);
self.body.frame = newlblFrame; //body is the textview
self.body.text = sourceCode;
self.scroll.contentSize=CGSizeMake(320, self.body.frame.size.height+300); //scroll is scrollview
A typical NSLog will display body frame = {{0, 0}, {320, 2088}} scrollview frame = {{0, 0}, {320, 417}} scrollview content size = {320, 2388}
However, when I run the app, body maintains its interface builder height of around 196 and the scrollview won't scroll (when i move it, it just bounces back to its original position)
On a side note, when I try to manually change the frame of the textview with CGRectMake, the NSLog shows the correct height, but the view doesn't appear different. I made sure that it's hooked up correctly in IB, because I can adjust other properties, like background color.
EDIT:
After I set the cliptoBounds property of the textview to NO, the textview now adjusts its height and tries to show the entire text. However, it cuts off at the end of the screen, and I still cannot scroll.
Here is what I see currently. I made the scrollview background color gray for convenience. I'm not sure why part of the scrollview is partially in white and and partially gray. (Title) is a separate label btw)
I fixed the problem by moving the code from viewDidLoad to viewDidAppear. Apparently this is an Xcode 4.5 specific issue, resulting from the AutoLayout feature overriding the logic in the viewDidLoad method. A better explanation can be found here.
i hope this will help you
self.scrollview.contentSize=CGSizeMake(320, label.frame.size.height+30);
Try to add this :
[_scrollView setClipsToBounds:YES];
Try this:
CGSize maximumSize = CGSizeMake(200.0, 30.0); // Specify your size. It was for my screen.
UIFont *txtFont = <Ur Font size & Var>;
//new
//fontValue = txtFont;
//new
lblValue.font = txtFont;
lblValue.lineBreakMode = UILineBreakModeWordWrap;
CGSize txtStringSize = [commentTxt sizeWithFont:txtFont
constrainedToSize:maximumSize
lineBreakMode:lblValue.lineBreakMode];
CGRect newlblFrame = CGRectMake(105.0, y, 200.0, txtStringSize.height);
lblValue.frame = newlblFrame;
lblValue.text = #"Your String";
After this set scroll Content size. Hope it'll work for you. It worked for me.
Try to solve in the following way.
// adjust the height of UITextView with content height
CGRect frame = self.body.frame;
frame.size.height = self.body.contentSize.height;
self.body.frame = frame;
// adjust UIScrollView's height with the height of UITextView
self.scroll.frame = frame;

Custom View with UILabel in it appears black

I have created a custom UIView that has some set of UILabels in it. I add those UILabels inside the custom view inside its drawRect method.
But the whole custom view appears black on the screen. I have not set any background color for the custom UIView. How Do I fix this?
I tried setting background color to ClearColor but it still looks black.
I tried setting opaque property of the custom view to false and the view obviously disappeared.
Please help.
don't do that in drawRect: method which is intended to draw in the graphic context. If you want to add some specific subviews do it in an init / initWithFrame method.
For me the best way is to create a custom uiviewcontroller subclass and initialize it using a xib (nib) file. Working at controller level is a good practice.
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(10,0,320,35)];
newView.backgroundColor=[UIColor clearColor];
UILabel *mytext = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 28.0)];
mytext.backgroundColor = [UIColor clearColor];
mytext.textColor = [UIColor blackColor];
mytext.editable = NO;
mytext.text = #"Your label";
[newView addSubview:mytext];
[mytext release];
[self.view addSubview:newView];
[newView release];
Just incase someone stumbles upon this thread like I did in 2021. Check to see if you have accidentally toggled 'dark mode'. It will show similar visual 'issues' to the question above.

Need Help Setting UITableViewCell's Background Image

I'm using a background image for my UITableViewCell.
If I just set the background of the cell, but keep everything else the same, I get this:
I'm if I use this code in viewDidLoad and it fixes the cell but it is making the navbar transparent:
self.navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
self.tableView.backgroundColor = [UIColor clearColor];
Is there way to get the navbar back and get the cell looking normal?
For the most part, I don't believe UIPopovers have many visual options as far as background.

How to mask a UILabel in iOS - Objective-C

I want to sub-class a UIView and place four UILabels over top one another ; top label will be the MASK, 2nd label will be a normal label with text, the 3rd label is a label with solid background with no text. the bottom label will the same as the top 2nd label with a different color font. when i sent the width of the third label it will cover up the bottom label showing a partial view of the text. I want to have the 2nd text be one color while the uncoverd bottom label display another color font.
Is this possibe? If someone can explain how to mask in objective-C that will help too.
I trying to build a UIView that acts like a progress bar, as the bar fill to 60%, I want to top text to show in white font color, when the bottom text shows in a different color.
You could do it with two UILabels, one on the bottom, and one embedded in another view on top.
UILabel *bottomLabel = ...;
[self.view addSubview:bottomLabel];
UIView *topContainer = [[UIView alloc] initWithFrame:bottomLabel.frame];
topContainer.clipsToBounds = YES;
topContainer.opaque = NO;
topContainer.backgroundColor = [UIColor clearColor];
UILabel *topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, bottomLabel.frame.size.width, bottomLabel.frame.size.height)];
topLabel.text = bottomLabel.text;
topLabel.opaque = NO;
topLabel.backgroundColor = [UIColor clearColor];
[topContainer addSubview:topLabel];
[self.view addSubview:topContainer];
Then, when you want to change the progress, you'd set the width of topContainer. This should clip topLabel.
Rather than using four UILabels, why not subclass UILabel and draw it yourself in the drawRect: method? It would look something like:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
// Set the mask
CGContextClipToMask(context, self.bounds, /* mask image */);
// Draw the text in a different font
[self.text drawInRect:rect withFont:/* alternate font */];
// Draw a solid background
CGContextSetRGBFillColor(context, ...);
CGContextFillRect(context, rect);
// Draw the text normally
[super drawRect:rect];
}
You could make the masking image and the alternate font properties of your subclass, for convenience.