Set the maximum character length of a UITextField

While the UITextField class has no max length property, it’s relatively simple to get this functionality by setting the text field’s delegate and implementing the following delegate method: Objective-C – (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { // Prevent crashing undo bug – see note below. if(range.length + range.location > textField.text.length) { return NO; } NSUInteger … Read more

iOS – Dismiss keyboard when touching outside of UITextField

You’ll need to add an UITapGestureRecogniser and assign it to the view, and then call resign first responder on the UITextField on it’s selector. The code: In viewDidLoad UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; [self.view addGestureRecognizer:tap]; In dismissKeyboard: -(void)dismissKeyboard { [aTextField resignFirstResponder]; } (Where aTextField is the textfield that is responsible for the keyboard) … Read more

How can I set the title of a UIButton as left-aligned?

Set the contentHorizontalAlignment: // Swift emailBtn.contentHorizontalAlignment = .left; // Objective-C emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; You might also want to adjust the content left inset otherwise the text will touch the left border: // Swift 3 and up: emailBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0); // Objective-C emailBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

How would I create a UIAlertView in Swift?

From the UIAlertView class: // UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead On iOS 8, you can do this: let alert = UIAlertController(title: “Alert”, message: “Message”, preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAlertAction(title: “Click”, style: UIAlertActionStyle.Default, handler: nil)) self.presentViewController(alert, animated: true, completion: nil) Now UIAlertController is a single class for creating and interacting with what … Read more

How do I size a UITextView to its content?

This works for both iOS 6.1 and iOS 7: – (void)textViewDidChange:(UITextView *)textView { CGFloat fixedWidth = textView.frame.size.width; CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)]; CGRect newFrame = textView.frame; newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height); textView.frame = newFrame; } Or in Swift (Works with Swift 4.1 in iOS 11) let fixedWidth = textView.frame.size.width let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, … Read more

How do I create a basic UIButton programmatically?

Here’s one: UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside]; [button setTitle:@”Show View” forState:UIControlStateNormal]; button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0); [view addSubview:button];

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)