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

Using HTML and Local Images Within UIWebView

Using relative paths or file: paths to refer to images does not work with UIWebView. Instead you have to load the HTML into the view with the correct baseURL: NSString *path = [[NSBundle mainBundle] bundlePath]; NSURL *baseURL = [NSURL fileURLWithPath:path]; [webView loadHTMLString:htmlString baseURL:baseURL]; You can then refer to your images like this: <img src=”https://stackoverflow.com/questions/747407/myimage.png”> (from … Read more

Move view with keyboard using Swift

Here is a solution, without handling the switch from one textField to another: override func viewDidLoad() { super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector(“keyboardWillShow:”), name: UIKeyboardWillShowNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector(“keyboardWillHide:”), name: UIKeyboardWillHideNotification, object: nil) } func keyboardWillShow(notification: NSNotification) { if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() { self.view.frame.origin.y -= keyboardSize.height } } func keyboardWillHide(notification: NSNotification) { self.view.frame.origin.y … Read more

UILabel text margin [duplicate]

I solved this by subclassing UILabel and overriding drawTextInRect: like this: – (void)drawTextInRect:(CGRect)rect { UIEdgeInsets insets = {0, 5, 0, 5}; [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)]; } Swift 3.1: override func drawText(in rect: CGRect) { let insets = UIEdgeInsets.init(top: 0, left: 5, bottom: 0, right: 5) super.drawText(in: UIEdgeInsetsInsetRect(rect, insets)) } Swift 4.2.1: override func drawText(in rect: CGRect) … Read more

How to Rotate a UIImage 90 degrees?

I believe the easiest way (and thread safe too) is to do: //assume that the image is loaded in landscape mode from disk UIImage * landscapeImage = [UIImage imageNamed:imgname]; UIImage * portraitImage = [[UIImage alloc] initWithCGImage: landscapeImage.CGImage scale: 1.0 orientation: UIImageOrientationRight]; Note: As Brainware said this only modifies the orientation data of the image – … Read more

How to find topmost view controller on iOS

I think you need a combination of the accepted answer and @fishstix’s + (UIViewController*) topMostController { UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController; while (topController.presentedViewController) { topController = topController.presentedViewController; } return topController; } Swift 3.0+ func topMostController() -> UIViewController? { guard let window = UIApplication.shared.keyWindow, let rootViewController = window.rootViewController else { return nil } var topController = … Read more

Adjust UILabel height depending on the text

sizeWithFont constrainedToSize:lineBreakMode: is the method to use. An example of how to use it is below: //Calculate the expected size based on the font and linebreak mode of your label // FLT_MAX here simply means no constraint in height CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX); CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode]; //adjust the label the … Read more

How to capture UIView to UIImage without loss of quality on retina display

Switch from use of UIGraphicsBeginImageContext to UIGraphicsBeginImageContextWithOptions (as documented on this page). Pass 0.0 for scale (the third argument) and you’ll get a context with a scale factor equal to that of the screen. UIGraphicsBeginImageContext uses a fixed scale factor of 1.0, so you’re actually getting exactly the same image on an iPhone 4 as … Read more