Placing Text in UITextField - iphone

textField.layer.backgroundColor = [UIColor lightGreenColorTransparent].CGColor;
textField.layer.cornerRadius = 8.0f;
textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
textField.layer.borderWidth = 1.0f;
How can I set the text for about 5px to the right that there is more space between the border and the beginning of the text?

UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;
set the leftView property for padding the UITextField.

drawTextInRect:
Draws the receiver’s text in the specified rectangle.
- (void)drawTextInRect:(CGRect)rect
Parameters
rect
The rectangle in which to draw the text.
Discussion
You should not call this method directly. If you want to customize the drawing behavior for the text, you can override this method to do your drawing.
By the time this method is called, the current graphics context is already configured with the default environment and text color for drawing. In your overridden method, you can configure the current context further and then invoke super to do the actual drawing or you can do the drawing yourself. If you do render the text yourself, you should not invoke super.
Availability
Available in iOS 2.0 and later.
Declared In
UITextField.h

textField.contentInset = UIEdgeInsetsMake(4,8,0,0);

Related

UIView layer shadow darkens after rotation

I have a UIView that I'm using as a header, and I've applied a drop shadow to this view. It gets onscreen as I intended, but after a rotation the shadow becomes twice as dark, as though it had been redrawn over top of itself. Subsequent rotations make it even darker. Here is the code for the header and shadow:
self.header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, HEADER_HEIGHT)];
self.header.backgroundColor = [UIColor whiteColor];
self.header.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
// Drop shadow.
self.header.layer.shadowColor = [UIColor blackColor].CGColor;
self.header.layer.shadowOpacity = 0.5;
self.header.layer.shadowRadius = 2.0;
self.header.layer.shadowOffset = CGSizeMake(0, 2);
// self.header.layer.shouldRasterize = YES;
// self.header.layer.rasterizationScale = [UIScreen mainScreen].scale;
This code all occurs in viewWillLayoutSubviews of my View Controller, and call [super willLayoutSubviews] at the beginning of the method. I have tried putting the same code in viewWillAppear:animated to no effect.
While writing this question out, I figured out the solution.
I was re-initializing the header every time viewWillLayout subviews was called.
Aside from being inefficient, this also caused the shadow to draw over itself for some reason.
I fixed this by initializing all of my views in loadView, and doing the frame setting in viewWillLayoutSubviews. This resolved the issue, and is nicer because I am not re-initializing objects that already exist.

How to extend keyboard gradient on iPhone?

I see that few apps are extending keyboard but I would like to know how they do it.
Here are 2 examples.
Textastic &
Prompt
Now I know that I can add inputAccessoryView to UITextView but it still has small thin line that separates keyboard from UIToolbar like on image bellow.
How they do it? Extending UIWindow that holds keyboard or in some other way?
Update 1 with answer:
So I have used solution that Tyraz wrote.
Subclass UIToolbar
Instead of image I have used UIView with background color same as the finishing color of the keyboard gradient and with UIViewAutoResizingMaskFlexibleWidth so that it covers keyboard when rotated, with height of 3 pixels
Here is the code for the subclassed UIToolbar
- (void)didMoveToSuperview {
[self.separatorHideView removeFromSuperview];
CGRect seperatorRect = CGRectMake(self.frame.origin.x,
self.frame.size.height,
self.frame.size.width,
3.0);
self.separatorHideView = [[UIView alloc]];
self.separatorHideView.backgroundColor = [UIColor colorWithRed:0.569 green:0.600 blue:0.643 alpha:1.000];
self.separatorHideView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self addSubview:self.separatorHideView];
}
Update 2: Here is code how I'm adding it to UITextView and what color I'm using for tint.
I'm adding it to the UITextView in viewDidLoad with following code
CustomToolbar *accessoryToolbar = [[CustomToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 38)];
accessoryToolbar.tintColor = [UIColor colorWithRed:0.569 green:0.600 blue:0.643 alpha:1.000];
editor.inputAccessoryView = accessoryToolbar;
And this is how it looks like with solution applied to it
On iOS 7 you can create an inputAccessoryView to match keyboard style easily:
[[UIInputView alloc] initWithFrame:<#frame#>
inputViewStyle:UIInputViewStyleKeyboard];
I would try to use a inputAccessoryView plus a second view that sits on top of the separator line and "fills the gap".
Once the inputAccessoryView is added to the keyboard (overwrite the didMoveToSuperview method in your accessory UIView subclass to get notified when this happens), add it to the inputAccessoryView's superview.
Should be something like that in your accessory UIView subclass:
- (void)didMoveToSuperview {
[self.separatorHideView removeFromSuperview];
CGRect seperatorRect = CGRectMake(self.frame.origin.x,
self.frame.origin.y + self.frame.size.height,
self.frame.size.width,
2.0);
UIImage *gapGradient = [UIImage imageNamed:#"GapGradient"];
self.separatorHideView = [[UIImageView alloc]initWithImage:gapGradient];
self.separatorHideView.frame = seperatorRect;
[self.superview addSubview:self.separatorHideView];
}
I would also overwrite setFrame in your accessory UIView subclass to update the frame of the gapView in case the frame of the keyboard is changed.

How to draw a line in interface builder in Xcode 4

I'd like to draw a simple inset line in Interface Builder to separate some items in a list and make the UI a bit more tidy. I don't see any "line" or similar objects in the objects library and can't seem to find any drawing commands in Interface builder.
I use a very narrow UIView with backgroundColor set to the appropriate color.
There are no lines in the iPhone UI library. This functionality on Max OS X was supplied by NSBox, but on the iPhone there is no corresponding UI element.
If you're afraid a UIView might affect performance, you can draw a line in code using CoreGraphics' CAShapeLayers.
Every UIView has a CALayer, so draw the line and add it to the views CALayer.
You can do this either in a custom UIView's drawRect or in your view controller:
(Make sure to add Quartz framework to your project)
UIBezierPath *linePath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0,self.view.frame.size.width, 1)];
//shape layer for the line
CAShapeLayer *line = [CAShapeLayer layer];
line.path = [linePath CGPath];
line.fillColor = [[UIColor blackColor] CGColor];
line.frame = CGRectMake(xPosition, yPosition, self.view.frame.size.width,1);
[self.view.layer addSublayer:line];
In place of view, why not just use a label and set the appropriate background color?
I used a view and made it narrow & changed the color to make it look like a line. But my issue is I am using the line on a vertical scrollview and the lines also get scrolled.
Interface Builder does not allow you to draw the shadows essential for an inset line. If you work with Photoshop you know this.
The following code will draw an "inset" if the line is in front of a white/beige Background.
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 1.0f)];
line.backgroundColor = [UIColor colorWithRed:(200.0f/255.0f) green:(200.0f/255.0f) blue:(200.0f/255.0f) alpha:1.0f];
line.layer.shadowColor = [UIColor darkGrayColor].CGColor;
line.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
line.layer.shadowRadius = 0.5f;
line.layer.shadowOpacity = 0.4f;
line.layer.masksToBounds =NO;
[Background addSubview:line];
Remember to import < QuartzCore/QuartzCore.h>.
As an alliterative - in Interface Builder, add a view who's height is set to 2 points. Wire up a IBOutlet to that view and set its layer border color and width:
self.mySeparatorLine.layer.borderWidth = 1.0f;
self.mySeparatorLine.layer.borderColor = [UIColor lightGrayColor].CGColor;
You can use Progress View as an option for a horizontal line. Just change the tint color and progress to 1 which is 100%.

How to display an array of UILabels?

I have a list of items I'd like to show in a UITableViewCell. Right now, I'm simply using a cell.textLabel with commas separating each value, but I'd like to do something a little more dynamic.
How would I achieve something like this?
Would it be an array of UILabels with borders and radius on those borders?
Thanks for your ideas.
Here's a possible quick and easy way to do this. It's based on the code that you can get here.
Note that you have to add the QuartzCore framework to your project and include in the file where you write this code!
Every UIView is backed by a CALayer. You can get at the UIView's CALayer with the .layer property. Since a UILabel is a UIView, you can get its backing layer this way. Once you have the backing layer, you can set its backgroundColor, cornerRadius, borderColor, and borderWidth properties. That should let you create the rounded effect.
To get the centered effect, try setting the UILabel's textAlignment to UITextAlignmentCenter. Then, you could try setting the UILabel's frame based on sizeThatFits, or maybe based on calling sizeWithFont on the string you're putting into the label.
Here's some quick, totally untested code to get you started.
Assume that somewhere you've initialized a UIFont as follows (put in whatever size you want for the font).
labelFont = [UIFont systemFontOfSize:14];
Then, for each label, set it up as follows.I'm assuming you've pulled the text out of an array and put into a variable called "text". X_PADDING and Y_PADDING are how much spacing you want around the label's text. xLoc and yLoc are variables you're using to keep track of the x and y position you want to put the labels at. You'll probably increase xLoc based on textSize + X_PADDING + LABEL_SPACING or something (where you define LABEL_SPACING):
CGSize textSize = [text sizeWithFont:labelFont];
CGRect frame = CGRectMake( xLoc, yLoc,
textSize.width + X_PADDING,
textSize.height + Y_PADDING);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = text;
label.textAlignment = UITextAlignmentCenter;
CALayer *layer = label.layer;
layer.masksToBounds = YES;
layer.cornerRadius = 7.0; // or whatever works for you
layer.borderWidth = 1.0;
layer.borderColor = [[UIColor redColor].CGColor;
layer.backgroundColor = [UIColor blueColor].CGColor;
// Add the layer into its superview
[yourSuperview addSubview:label];
I hope this helps get you started.

How to draw an UILabel in -drawRect:?

Must I also do all this crazy coordinate system conversion stuff here, or is an UILabel different from an UIImageView drawing in -drawRect: ?
There's a method called - (void)drawTextInRect:(CGRect)rect for that.
BUT the documentation says: "You should not call this method directly. This method should only be overridden by subclasses that want to modify the default drawing behavior for the label’s text."
So? How to draw it then in -drawRect:?
UILabel is different in that you don't need to manually draw text to alter the way it is presented. Subclassing UILabel and overriding -drawTextInRect: is the quickest way to alter the way a UILabel is rendered. For example,
- (void)drawTextInRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShadowWithColor( context, shadowOffset, shadowRadius, [shadowColor CGColor] );
[super drawTextInRect:rect];
}
will add a shadow with a specific offset, radius, and color (as a UIColor instance) to any text that you draw in that UILabel. For an example of this in action, see a project I put together for a recent class.
However, if what you are looking to do is just draw text within another view, Vladimir's answer is the way to go.
If you perform custom drawing in your view you must not draw UILabel or another UI element but rather draw text itself. NSString has several methods for drawing in current context (look at NSString's UIKit extension docs for more methods):
- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font
- (CGSize)drawAtPoint:(CGPoint)point withFont:(UIFont *)font
Actually you can do it.
Try this:
UILabel *lblRef = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
lblRef.text = [refs objectAtIndex:barCount];
lblRef.adjustsFontSizeToFitWidth = TRUE;
lblRef.adjustsLetterSpacingToFitWidth = TRUE;
lblRef.textColor = self.color;
[lblRef setTextAlignment:NSTextAlignmentCenter];
lblRef.backgroundColor = [UIColor clearColor];
[self addSubview:lblRef];