Line appears in iOS 7 Textfield - ios

I have used the below code to display a colored placeholder within iOS Textfield. This works fine in iOS 5 and 6. But in iOS 7 a line appears in the middle of the Textfield.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void) drawPlaceholderInRect:(CGRect)rect {
[SEARCH_FIELD_FONT_COLOR setFill];
if (self.placeholderFont) {
[[self placeholder] drawInRect:rect withFont:SEARCH_FIELD_FONT];
}else {
[[self placeholder] drawInRect:rect withFont:self.placeholderFont];
}
}
#end

Try with following code for change color of placeholder of textFiled.
[self.myTextField setValue:[UIColor darkGrayColor] forKeyPath:#"_placeholderLabel.textColor"];

This could work a bit better. Also, you can change the font using and attributed placeholder.
- (void) drawPlaceholderInRect:(CGRect)rect {
[SEARCH_FIELD_FONT_COLOR setFill];
[super drawPlaceholderInRect:rect];
}

Related

Limit the text input boundaries on iOS

I am developing an app for my company but i am no expert on obj-C programming language. I've tried to find some answers on the internet but no success at all. The solution to the problem may be simple, but i am unable to solve it.
I want to limit the text so it fits inside the text box. What is happening right now is as i start typing and it flows on the first line forever and do not change drop to the second line when it hits the box boundaries.
otherdetails = [[UITextField alloc] initWithFrame:otherdetailsf];
otherdetails.text = otherdetailstxt;
[otherdetails.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[otherdetails.layer setBorderWidth:2.0];
otherdetails.layer.cornerRadius = 5;
otherdetails.clipsToBounds = YES;
otherdetails.delegate = self;
otherdetails.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
otherdetails.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
Thats what happens when i type a lot on the text field....
How can i solve this problem?
Thanks!!
Use TextViewinstead of text field if you need to support multiple lines of text whenever the text reaches the end of a line.
From Documentation:
The UITextView class implements the behavior for a scrollable,
multiline text region. The class supports the display of text using
custom style information and also supports text editing. You typically
use a text view to display multiple lines of text, such as when
displaying the body of a large text document.
So your code should be:
otherDetails=[[UITextView alloc]initWithFrame:otherdetailsf];
otherDetails.text=#"";
[otherDetails.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[otherDetails.layer setBorderWidth:2.0];
otherDetails.layer.cornerRadius = 5;
otherDetails.clipsToBounds = YES;
otherDetails.delegate = self;
// otherDetails.contentMode=UIControlContentVerticalAlignmentTop;
try this code:
in viewcontroller.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITextViewDelegate>
#property (strong,nonatomic)IBOutlet UITextView *mytextview;
#end
in viewcontroller.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize mytextview;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)addTextView
{
[mytextview setText:#"Lorem ipsum dolor sit er elit lamet"];
mytextview.delegate = self;
[self.view addSubview:mytextview];
mytextview.delegate = self;
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:
(NSRange)range replacementText:(NSString *)text{
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
}
return YES;}
-(void)textViewDidBeginEditing:(UITextView *)textView{
NSLog(#"Did begin editing");
}
-(void)textViewDidChange:(UITextView *)textView{
NSLog(#"Did Change");
}
-(void)textViewDidEndEditing:(UITextView *)textView{
NSLog(#"Did End editing");
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
[textView resignFirstResponder];
return YES;
}
#end

UITableViewCell reorder clears subview's background color

This question has been asked few times but there don't seem to be any solution to this.
UITableView reorder hides background
Subviews of UITableViewCell are not visible while reordering
Reordering causing subview 's backround color to clear
I have a custom tableview cell with UILabel in it. When tableview is in edit mode and I drag the cell to reorder, UILabel's background becomes clear color. I've also found that, if I try to reorder selected cell (my tableview is allowed multiple selection during edit mode), the subview's background color stays.
I tried below methods in my CustomCell but none of them overrides the subview's background color when cell is being dragged.
I want the subview's background color to stay. Is there any method that I missed? Or Apple designed it this way?
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected) {
if (self.isEditing) {
self.customLabel.backgroundColor = [UIColor blueColor];
}
else {
self.customLabel.backgroundColor = [UIColor blueColor];
}
}
else {
self.customLabel.backgroundColor = [UIColor blueColor];
}
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
if (self.isEditing) {
self.customLabel.backgroundColor = [UIColor blueColor];
}
else {
self.customLabel.backgroundColor = [UIColor blueColor];
}
}
else {
self.customLabel.backgroundColor = [UIColor blueColor];
}
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if (editing) {
self.customLabel.backgroundColor = [UIColor blueColor];
}
else {
self.customLabel.backgroundColor = [UIColor blueColor];
}
}
Adding a modern solution for Swift 3.
Create a new .swift file and make a custom UIView class:
import UIKit
class NeverClearView: UIView {
override var backgroundColor: UIColor? {
didSet {
if backgroundColor?.cgColor.alpha == 0 {
backgroundColor = oldValue
}
}
}
}
In Interface Builder, go to the Identity Inspector and set the class to your shiny new NeverClearView class. This will halt it from disappearing during cell reordering.
This solution also works for other UIKit components, for example I had to do the same thing for a UIStepper a second ago.
You may create your custom UILabel. And overload -drawRect.
#interface VALAbel:UILabel
#property (nonatomic, strong) UIColor *bac_color;
#end
#implementation VALabel
-(void)setBac_color:(UIColor *)bac_color
{
_bac_color = bac_color;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
[self.bac_color set];
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), true);
CGContextFillRect(UIGraphicsGetCurrentContext(), self.bounds);
[super drawRect:rect];
}
#end
This will help you!

iOS7 Selected Effect on Button Border

So I am new to iOS and I want some buttons with rounded boarders. I also want those borders to have the same effect as the text within the button when the button is selected.
Since roundRect buttons are no longer an object in iOS (or at least I couldn't find it and everywhere I read said that is was no more) I decided to write a custom class that extends UIButton. This is what I have:
- (void)drawRect:(CGRect)rect{
{
UIColor *blackColor = blackColor;
UIColor *transBlack = [blackColor colorWithAlphaComponent:(0.5)];
[self.layer setCornerRadius:10.0f];
[self.layer setBorderColor:[UIColor blackColor].CGColor];
[self.layer setBorderWidth:1.0];
if(self.isSelected){
[self.layer setBorderColor:(transBlack.CGColor)];
}
I'm not sure if I am using isSelected properly. I had an NSLog under it and it never seems to be executed no matter how many times I press the buttons.
Any help and suggestions of all kinds would be greatly appreciated. Thank you.
UIButton inherit from UIView, So you can use Layer's Methods...
Create new Object that Subclassing UIButton call it whatever you want, then implement the next Code:
In this sample i have UIButton subclass called PressedButton in the .m file:
#implementation PressedButton
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
//When the button is pressed the border color change to red
self.layer.borderColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5].CGColor;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
//When the button is pressed the border color change back to black
self.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5].CGColor;
}
- (void)initialize{
self.layer.cornerRadius = 10.0f;
self.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5].CGColor;
self.layer.borderWidth = 2;
self.layer.masksToBounds = YES;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
[self initialize];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initialize];
}
return self;
}
- (instancetype)init
{
self = [super init];
if (self) {
[self initialize];
}
return self;
}
#end
*** I implemented the all init methods to be sure no matter where i set the button (storyboard or via Code its get the same init).
After that just configure your button custom class to your "Pressed Button class" and that's it
If you need more help be free to ask :)

The background colour of a grouped UITableView will not go transparent in iOS7

I am working on an app, which worked perfectly fine with iOS 6. With iOS7 the app has couple of layout and general few appearance issues. One is not being able to set the background color of a grouped tableview to transparent. Heres is my code which does not work anymore
tableForDetails = [[UITableView alloc]initWithFrame:CGRectMake(0, yAxisTable, 320, 150) style:UITableViewStyleGrouped];
UIColor *backGroundColor = [UIColor clearColor];
UIView *bview = [[UIView alloc]init];
[bview setBackgroundColor:backGroundColor];
[tableForDetails setBackgroundView:bview];
Help is greatly appreciated. Thanks
To set background transparency for a grouped uiTableview in iOS7 is really quite easy. The solution is even more simple than I originally thought.
[tableForDetails setBackgroundColor:[UIColor clearColor]];
or for only semi transparent
[tableForDetails setBackgroundColor:[[UIColor alloc]initWithRed:1.0 green:1.0 blue:1.0 alpha:0.5]];
This worked fine for me.
I fixed it with this:
tableView.backgroundView = UIView.new;
I put any kind of improvements on Apple components in a super class. The following upgrades are applied in BasicTableView.m, which extends UITableView:
1) overrides setBackgroundColor to work with iOS 7 and above, while maintaining backward compatibility with iOS 6 and 5.
2) overrides initWithCoder: and initWithFrame:style: to initialize the background color to a default transparent background
- (id) initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
if (self) {
[self setup];
}
return self;
}
- (id) initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
self = [super initWithFrame:frame style:style];
if (self) {
[self setup];
}
return self;
}
- (void) setup
{
//Set the BG color to clear color by default
[self setBackgroundColor:[UIColor clearColor]];
}
- (void) setBackgroundColor:(UIColor *)backgroundColor
{
if (self.style == UITableViewStyleGrouped) {
if (majorSystemVersion() < 7) {
UIView *tableBgView = [[UIView alloc] init];
tableBgView.backgroundColor = backgroundColor;
self.backgroundView = tableBgView;
} else {
[super setBackgroundColor:backgroundColor];
}
}
}
//majorSystemVersion() is defined elsewhere in a utility file
int majorSystemVersion(void) {
NSString *systemVersion = [UIDevice currentDevice].systemVersion;
NSArray *systemVersionComps = [systemVersion componentsSeparatedByString:#"."];
NSString *majorVersion = [systemVersionComps objectAtIndex:0];
return [majorVersion intValue];
}
UITableViewCell is in white Background by default in iOS 7
put this inside your cellforRowAtIndexPath
[cell setBackgroundColor:[UIColor clearColor]]

UIImage resizableImageWithCapInsets - can this be specified using a nib?

I have a UIImage resizing perfectly using resizableImageWithCapInsets to make a badge containing a text label. I'm interested to know is there a way to do this in a storyboard, or in a view created with a nib?
There's no technical reason to require this. Most of the UI is done in the nib and I would like to keep as much there as possible rather than loading the image, making it resizable, finding the text dimensions, resizing the UIImageView and then applying the label in code.
I can't make this project iOS6-only yet (deployment target ios5+) so using layout constraints isn't an option.
Yes, there is.
The trick is to override -initWithCoder: and the image setters (setImage:forState: and setBackgroundImage:forState:) to create the resizable versions of the same image you set in the xib. Obviously, they still won't resize properly in xib itself, but it will be resized properly after initialization. Don't forget to set NAResizableButton as the custom class of UIButton.
Sure beats doing it programmatically. :)
Interface:
#import <UIKit/UIKit.h>
#interface NAResizableButton : UIButton
#end
Implementation:
#import "NAResizableButton.h"
#implementation NAResizableButton
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self setImage:[self imageForState:UIControlStateNormal] forState:UIControlStateNormal];
[self setImage:[self imageForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
[self setImage:[self imageForState:UIControlStateSelected] forState:UIControlStateSelected];
[self setImage:[self imageForState:UIControlStateDisabled] forState:UIControlStateDisabled];
[self setBackgroundImage:[self backgroundImageForState:UIControlStateNormal] forState:UIControlStateNormal];
[self setBackgroundImage:[self backgroundImageForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
[self setBackgroundImage:[self backgroundImageForState:UIControlStateSelected] forState:UIControlStateSelected];
[self setBackgroundImage:[self backgroundImageForState:UIControlStateDisabled] forState:UIControlStateDisabled];
}
return self;
}
- (void)setImage:(UIImage *)inImage forState:(UIControlState)inState
{
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(ceilf(inImage.size.height / 2), ceilf(inImage.size.width / 2), ceilf(inImage.size.height / 2), ceilf(inImage.size.width / 2));
if ([inImage respondsToSelector:#selector(resizableImageWithCapInsets:)])
{
// iOS 5
inImage = [inImage resizableImageWithCapInsets:edgeInsets];
}
else
inImage = [inImage stretchableImageWithLeftCapWidth:edgeInsets.left topCapHeight:edgeInsets.top];
[super setImage:inImage forState:inState];
}
- (void)setBackgroundImage:(UIImage *)inImage forState:(UIControlState)inState
{
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(ceilf(inImage.size.height / 2), ceilf(inImage.size.width / 2), ceilf(inImage.size.height / 2), ceilf(inImage.size.width / 2));
if ([inImage respondsToSelector:#selector(resizableImageWithCapInsets:)])
{
// iOS 5
inImage = [inImage resizableImageWithCapInsets:edgeInsets];
}
else
inImage = [inImage stretchableImageWithLeftCapWidth:edgeInsets.left topCapHeight:edgeInsets.top];
[super setBackgroundImage:inImage forState:inState];
}
#end

Resources