Center text vertically in a UITextView

First add an observer for the contentSize key value of the UITextView when the view is loaded: – (void) viewDidLoad { [textField addObserver:self forKeyPath:@”contentSize” options:(NSKeyValueObservingOptionNew) context:NULL]; [super viewDidLoad]; } Then add this method to adjust the contentOffset every time the contentSize value changes: -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { UITextView *tv = object; … Read more

iOS7 UITextView contentsize.height alternative

With this following code, you can change the height of your UITextView depending of a fixed width (it’s working on iOS 7 and previous version) : – (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text andWidth:(CGFloat)width { UITextView *textView = [[UITextView alloc] init]; [textView setAttributedText:text]; CGSize size = [textView sizeThatFits:CGSizeMake(width, FLT_MAX)]; return size.height; } With this function, you will take a … Read more

UITextView text content doesn’t start from the top

I had the same problem, and turns out I had to set the content offset in viewDidLayoutSubviews for it to take effect. I’m using this code to display attributed static text. – (void)viewDidLayoutSubviews { [self.yourTextView setContentOffset:CGPointZero animated:NO]; } SWIFT 3: override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() self.textView.setContentOffset(CGPoint.zero, animated: false) }

Limit number of characters in uitextview

– (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { return textView.text.length + (text.length – range.length) <= 140; } This accounts for users cutting text, or deleting strings longer than a single character (ie if they select and then hit backspace), or highlighting a range and pasting strings shorter or longer than it. Swift 4.0 Version func textView(_ … Read more

Display html text in uitextview

Use following block of code for ios 7+. NSString *htmlString = @”<h1>Header</h1><h2>Subheader</h2><p>Some <em>text</em></p><img src=”http://blogs.babble.com/famecrawler/files/2010/11/mickey_mouse-1097.jpg” width=70 height=100 />”; NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding] options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes: nil error: nil ]; textView.attributedText = attributedString;

IOS – remove ALL padding from UITextView

Although it is iOS 7 only, an extremely clean solution is to set the textView’s textContainerInsets as such: [textView setTextContainerInset:UIEdgeInsetsZero]; textView.textContainer.lineFragmentPadding = 0; // to remove left padding This will effectively remove all padding (insets) around the text inside the text view. If your deployment target is iOS 7+ then this is the best solution … Read more

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