Create UITextRange from NSRange

You can create a text range with the method textRangeFromPosition:toPosition. This method requires two positions, so you need to compute the positions for the start and the end of your range. That is done with the method positionFromPosition:offset, which returns a position from another position and a character offset. – (CGRect)frameOfTextRange:(NSRange)range inTextView:(UITextView *)textView { UITextPosition … Read more

HTML Format in UITextView

The problem there is that you have to change the Character Encoding options from NSUnicodeStringEncoding to NSUTF8StringEncoding to load your of your html the proper way. I think you should create a string extension read-only computed property to convert your html code to attributed string: Xcode 8.3.1 • Swift 3.1 extension Data { var attributedString: … Read more

Resize font size to fill UITextView?

I have converted dementiazz’s answer to Swift: func updateTextFont() { if (textView.text.isEmpty || CGSizeEqualToSize(textView.bounds.size, CGSizeZero)) { return; } let textViewSize = textView.frame.size; let fixedWidth = textViewSize.width; let expectSize = textView.sizeThatFits(CGSizeMake(fixedWidth, CGFloat(MAXFLOAT))); var expectFont = textView.font; if (expectSize.height > textViewSize.height) { while (textView.sizeThatFits(CGSizeMake(fixedWidth, CGFloat(MAXFLOAT))).height > textViewSize.height) { expectFont = textView.font!.fontWithSize(textView.font!.pointSize – 1) textView.font = expectFont } … Read more

UITextView cursor below frame when changing frame

Instead of resizing the frame, why not give your text view a contentInset (and a matching scrollIndicatorInsets)? Remember that text views are actually scrollviews. This is the correct way to handle keyboard (or other) interference. For more information on contentInset, see this question. This seems to not be enough. Still use insets, as this is … Read more

Get X and Y coordinates of a word in UITextView

UITextView conforms to UITextInput, of which a detailed description can be found here. Take a look at the required methods “textRangeFromPosition:toPosition:”, “positionFromPosition:offset:”, “positionFromPosition:inDirection:offset:”, and some of the other geometric-based methods in the UITextInput Protocol. Those might provide the functionality you are looking for. I have not actually tried to make sure these work the way … Read more

Hyperlinks in a UITextView

Use NSAttributedString NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@”Google” attributes:@{ NSLinkAttributeName: [NSURL URLWithString:@”http://www.google.com”] }]; self.textView.attributedText = attributedString; Sure, you can set just a portion of the text to be the link. Please read more about the NSAttributedString here. If you want to have more control and do something before opening the link. You can set the … Read more