iOS 7.1 UITextView still not scrolling to cursor/caret after new line

Improved solution’s code for UITextView descendant class: #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) #define is_iOS7 SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@”7.0″) #define is_iOS8 SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@”8.0″) @implementation MyTextView { BOOL settingText; } – (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextViewDidChangeNotification:) name:UITextViewTextDidChangeNotification object:self]; } return self; } – (void)scrollToCaretInTextView:(UITextView *)textView animated:(BOOL)animated { CGRect rect … Read more

How to add CSS for HTML to NSAttributedString?

Since iOS 7 you can use NSAttributedString with HTML syntax: NSURL *htmlString = [[NSBundle mainBundle] URLForResource: @”string” withExtension:@”html”]; NSAttributedString *stringWithHTMLAttributes = [[NSAttributedString alloc] initWithFileURL:htmlString options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil]; textView.attributedText = stringWithHTMLAttributes; // attributedText field! You have to add string.html to you project, and the content of the html can be like this: <html> <head> <style type=”text/css”> … Read more

How to resize UITextView on iOS when a keyboard appears?

The best result was reached by the following code. Also don’t forget to set background color to UIView and place UITextView before other top-screen controls (e.g. UITabBar). Editing of a text in the end still isn’t perfect now. You may try to improve. FirstViewController.h: @interface FirstViewController : UIViewController { IBOutlet UIBarButtonItem *buttonDone; IBOutlet UITextView *textView; … Read more

UITableViewCell, UITextView with dynamic height

Use UILabel for your cell text. You can then use sizeWithFont:constrainedToSize: to calculate the height of that UILabel within each cell. For example: #define PADDING 10.0f – (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *text = [self.items objectAtIndex:indexPath.row]; CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width – PADDING * 3, 1000.0f)]; return textSize.height + PADDING * 3; … Read more

UITextView is not scrolled to top when loaded

add the following function to your view controller class… Swift 3 override func viewDidLayoutSubviews() { self.mainTextView.setContentOffset(.zero, animated: false) } Swift 2.1 override func viewDidLayoutSubviews() { self.mainTextView.setContentOffset(CGPointZero, animated: false) } Objective C – (void)viewDidLayoutSubviews { [self.mainTextView setContentOffset:CGPointZero animated:NO]; }