UITextField __CFStringEncodeByteStream EXC_BAD_ACCESS - ios

I have a strange exception on a UITextField. I solved it but have no clue as to why it does.
After running the application I set the UITextField appearance as follows:
UITextField *textfieldAppearance = [UITextField appearance];
textfieldAppearance.font = [_theme textfieldFont];
textfieldAppearance.textColor = [_theme textfieldColor];
if (textDirectionRTL) {
textfieldAppearance.textAlignment = NSTextAlignmentRight;
}
textfieldAppearance.backgroundColor = [UIColor clearColor];
As you can see, there's nothing unusual about it, but when the controller with the UITextField opens I get a __CFStringEncodeByteStream + 17 EXC_BAD_ACCESS exception.
The solution was to comment out the backgroundColor setting.
I have the exact same appearance setting on UITextView and everything's ok.
Moreover, when I set the UITextField backgroundColor in the code within awakeAfterUsingCoder, everything works like a charm.
I'm running the app in XCode Version 4.6.3 (4H1503) simulator
Any clue to this strange behavior?

This is crashing because you are using the appearance proxy, which doesn't support all customisations. It doesn't seem to support backgroundColor so you will have to modify that on each text field individually

First of all why are u using UIAppearance class to customize the UItextfield..?? All the properties u are trying to set are present in UItextField itself...!
textField.backgroundColor = [UIColor clearColor];
textField.textColor = [UIColor redColor];
textField.textAlignment = UITextAlignmentCenter;
textField.font = [UIFont fontWithName:#"font name" size:12.0];
//Set the properties as you want.
Refer here for more

Related

Objective c - new UILabel text is 'merged' with the existing

I am refactoring an 'old' Objective-C iOS-program. All layout is done programmatically without storyboard.
I am experiencing some odd behavior when I try to update the text on a UILabel. The text is NOT updated but written 'on top' of the existing text.
The context is:
The UILabel is defined in a 'top-bar' that is managed by a sub-ViewController. It is assigned as 'default'/placeholder value when initialized by the sub-ViewController.
lblArea = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
//self.lblArea.text = #"giraf";
lblArea.textColor = BLACK_COLOR;
lblArea.backgroundColor = [UIColor clearColor];
lblArea.textAlignment = NSTextAlignmentCenter;
lblArea.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:14];
//self.lblArea.text=SEARCH_FOR_ADDRESS;
NSAttributedString *placeholderText = [[NSMutableAttributedString alloc] initWithString:SEARCH_FOR_ADDRESS attributes : #{
NSForegroundColorAttributeName : GRAY_COLOR,
}];
lblArea.attributedText=placeholderText;
[self.view addSubview:lblArea];
The setter-method is
-(void)setLabelAreaText:(NSString *)labelText{
lblArea.text=labelText;
}
In the parent controller the sub-controller is added as follows:
[self addChildViewController:topBarViewController];
[topBarViewController didMoveToParentViewController:self];
[self.view addSubview:[topBarViewController view]];
When the label is assigned another value from the parent-ViewController (via a setter method) the text is not replaced but written 'on top' of the existing.
The UILabel is defined as a instance variable and is made accesiable via a custom setter-method. I have tried to change it to a public property but it doesn't change anything.
Can anyone please help with an explanation :-)

Keep a hardware (bluetooth) keyboard from overriding an inputView in iOS 8

I have a UITextField that uses a custom UIPickerView in its inputView to restrict the user to a few options, functioning analogous to a dropdown list. When used with a hardware keyboard connected via bluetooth, the hardware keyboard overrides (and hides) my inputView. The associated inputAccessoryView remains, however. When press the "keyboard" button on my hardware keyboard (which would normally display the default on-screen keyboard for a vanilla UITextField), everything works as expected. (By which I mean my inputView is visible again.) In prior version of iOS (i.e. 7 and below), the inputView was always visible when called.
I have temporarily solved the problem by setting the UITextField's UIKeyboardType from UIKeyboardTypeDefault to UIKeyboardTypeTwitter, but this will only work while the hardware keyboard is not recognized as the default for that particular UIKeyboardType. Code is pickerTextField.keyboardType = UIKeyboardTypeTwitter; (See about 2/3 of the way into the code below.) Upon further testing, this partial solution isn't nearly as useful as it seems.
How can I correctly display my inputView in all situations while using a hardware keyboard?
Relevant code snippet:
-(int)setUpPickerWheelAtXLoc:(int)xLoc andYLoc:(int)yLoc {
int qwidth = width - (xLoc - FORMBORDERLEFT);
// Set up inputView (pickerView) to replace the keyboard
self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(xLoc, yLoc, qwidth, height)];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
self.pickerTextField = [[UITextField alloc] initWithFrame:CGRectMake(xLoc, yLoc, qwidth, 32)];
self.pickerTextField.delegate = self;
pickerTextField.borderStyle = UITextBorderStyleRoundedRect;
pickerTextField.layer.borderWidth = 0.3f;
pickerTextField.layer.borderColor = [[UIColor blackColor] CGColor];
pickerTextField.textColor = [UIColor blackColor];
pickerTextField.font = [UIFont systemFontOfSize:XXLARGEFONTSIZE];
pickerTextField.placeholder = #"Tap to choose from list...";
// Add pickerView as inputView
pickerTextField.inputView = self.pickerView;
pickerTextField.keyboardType = UIKeyboardTypeTwitter; // Suggested temporary solution
// Setup inputAccessoryView (Done button)
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[doneButton addTarget:self action:#selector(pickerDoneButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[doneButton setTitle:#"Done" forState:UIControlStateNormal];
doneButton.titleLabel.font = [UIFont systemFontOfSize:XXLARGEFONTSIZE];
doneButton.frame = CGRectMake(0,0,100,44);
[doneButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
doneButton.contentEdgeInsets = UIEdgeInsetsMake(0,0,0,20);
doneButton.backgroundColor = [UIColor lightGrayColor];
pickerTextField.inputAccessoryView = doneButton;
return xLoc + qwidth;
}
Additional Info: After checking a few other places, I have confirmed that this is happening in at least one Apple product as well. (In the "Create New Apple ID" app/page on an iPhone 6 in 8.0.2. See the Date of Birth pickerviews for Month and Day.)
The issue was fixed by Apple in the iOS 8.1 update.

Why is my NSAttributed String losing its attributes when the view reappears?

I am using UIAppearanceProxy in my app, like so:
UILabel *label = [UILabel appearance];
label.font = myAppFont;
label.textColor = myAppTextColor;
Sometimes, though, I want to override the default font to use glyphs from Awesome Font. I do that like this:
NSString *basePos = [NSString stringWithFormat:#"%d %#", (int)self.user.positives, kUpArrow];
NSMutableAttributedString *posString = [[NSMutableAttributedString alloc] initWithString:basePos];
[posString setAttributes:#{NSFontAttributeName:[UIFont fontWithName:#"FontAwesome" size:18]} range:NSMakeRange([posString length] - 1, 1)];
[self.positivesLabel setAttributedText:posString];
This works great when the view is built and appears, but if I navigate to another page (I am using UIPageViewController) and back, my attributes are gone.
I've tried re-setting-up the attributes in:
viewWillAppear:
viewDidAppear:
viewWillLayoutSubviews
viewDidLayoutSubviews
...to no avail. When is this string getting reset to the default? And how can I override that?
A similar thing was happening in to me.
The reason that this was happening was because I had set the font property on the appearance proxy of UILabel, and for whatever reason that overtook the attributes of the attributed string when the view re-appeared.
I fixed this by setting the font property on the UILabel to nil before setting the attributedText. Note that setting the font to any font also fixed this issue; it does not necessarily need to be set to nil.

Remove tint on TableView iOS 7

I am trying to remove the white view behind the alphabet indexer but have had no luck.
I am trying to make it clear but nothing has worked for me so far.
sectionIndexBackgroundColor is what your looking for. You can do something like this,
tableView.sectionIndexBackgroundColor = [UIColor clearColor];
If you want to change the color of the highlighted state, use the following property.
tableView.sectionIndexTrackingBackgroundColor = [UIColor redColor];
Keep in mind that sectionIndexBackgroundColor became available in iOS7. So if your supporting iOS6 you might want to call something like this.
if ([tableView respondsToSelector:#selector(sectionIndexBackgroundColor)]) {
tableView.sectionIndexBackgroundColor = [UIColor clearColor];
}

How to change the font size of the text on a UISegmentedControl?

Following is the code for initializing my UISegmentedControl.
- (void)initializeToolButtons
{
NSArray *buttonTitles = [NSArray arrayWithObjects:#"ANNEXET", #"HOVET", #"GLOBEN", "ALL", nil];
toolbuttons = [[UISegmentedControl alloc] initWithItems:buttonTitles];
toolbuttons.segmentedControlStyle = UISegmentedControlStyleBar;
toolbuttons.tintColor = [UIColor darkGrayColor];
toolbuttons.backgroundColor = [UIColor blackColor];
toolbuttons.frame = CGRectMake(0, 0, 320, 30);
[toolbuttons addTarget:self action:#selector(toolButtonsAction) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:toolbuttons];
}
How can I reduce the font size for each item on the UISegmentedControl?
Note: toolButtons has already been declared globally.
Very simple:
UIFont *Boldfont = [UIFont boldSystemFontOfSize:16.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:Boldfont forKey:UITextAttributeFont];
[segment setTitleTextAttributes:attributes forState:UIControlStateNormal];
Consider re-designing your interface or use the "tab" style which has a smaller font. Messing with unexposed properties might get your app rejected or break your app if they change something under the hood.
For example the code sample given doesn't work. When you tap on a segment the font for that segment gets reset to its normal size. Anything unpredictable can happen or change in your app if you do things that deviate from the normal useage of these things. So if you want an app that will continue working in following OS updates stick with the standard stuff, or make your own controls with UIButtons and rectangular background images. A hack might work now, but its not to say it will in the future.

Resources