Scroll to a specific part of a UITextView in Swift - ios

In the Swift app I'm creating, the user can type some text into a text view and then search for a specific string within it, just like in Pages (and numerous others).
I have the index of the character they are searching for as an Integer (i.e., they are on the third instance of "hello" in the massive text block they typed in, and I know that's the 779th letter of the text view) and I am trying to automatically scroll the text view to the string they're on, so that it pops out (like it would in Pages).
I am trying to jump to the applicable string with this command:
self.textview.scrollRangeToVisible(NSMakeRange(index, 0))
but it never jumps to the right place (sometimes too far down, sometimes way too far up), often depending on screen size, so I know that index doesn't belong right there.
How can I correctly allow the user to jump to a specific string in a text view, by making the text view scroll to a certain character?

You can get string, which is before index 779, and then calculate the height of this string and then scroll to this point.
let string = textView.text.substringWithRange(Range<String.Index>(start: textView.text.startIndex, end: textView.text.startIndex.advancedBy(779)))// your <779 string
let size = string.sizeWithAttributes([NSFontAttributeName:yourFont])
let point = CGPointMake(0, size.height)
scrollView.setContentOffset(point, animated:true)

The other answer doesn't seem to work anymore (Jan 2021). But there is a simpler way now:
// Range specific to the question, any other works
let rangeStart = textView.text.startIndex
let rangeEnd = textView.text.index(rangeStart, offsetBy: 779)
let range = rangeStart..<rangeEnd
// Convert to NSRange
let nsrange = NSRange(range, in: textView.text)
// ... and scroll
textView.scrollRangeToVisible(nsrange)

Related

How to split NSAttributedString across equal bounds views with correct word wrap

I have been grappling with this since a while. There are APIs that give us bounds size for given attributes of NSAttributedString.
But there is no direct way to get string range that would fit within given bounds.
My requirement is to fit very long string across a few paged views (PDF is not an option, neither is scrolling). Hence I have to figure out string size for each view (same bounds).
Upon research I found that CTFramesetterSuggestFrameSizeWithConstraints and its friends in Core Text maybe of help. I tried the approach described here, but the resulting ranges have one ugly problem:
It ignores word breaks (a different problem unrelated to Core Text, but I would really like to see if there was some solution to that as well).
Basically I want paging of text across number of UITextView objects, but not getting the right attributed string splits.
NOTE:
My NSAttributedString attributes are as follows:
let attributes: [NSAttributedString.Key : Any] = [.foregroundColor : textColor, .font : font, .paragraphStyle : titleParagraphStyle]
(titleParagraphStyle has lineBreakMode set to byWordWrapping)
extension UITextView
{
func getStringSplits (fullString: String, attributes: [NSAttributedString.Key:Any]) -> [String]
{
let attributeString = NSAttributedString(string: fullString, attributes: attributes)
let frameSetterRef = CTFramesetterCreateWithAttributedString(attributeString as CFAttributedString)
var initFitRange:CFRange = CFRangeMake(0, 0)
var finalRange:CFRange = CFRangeMake(0, fullString.count)
var ranges: [Int] = []
repeat
{
CTFramesetterSuggestFrameSizeWithConstraints(frameSetterRef, initFitRange, attributes as CFDictionary, CGSize(width: bounds.size.width, height: bounds.size.height), &finalRange)
initFitRange.location += finalRange.length
ranges.append(finalRange.length)
}
while (finalRange.location < attributeString.string.count)
var stringSplits: [String] = []
var startIndex: String.Index = fullString.startIndex
for n in ranges
{
let endIndex = fullString.index(startIndex, offsetBy: n, limitedBy: fullString.endIndex) ?? fullString.endIndex
let theSubString = fullString[startIndex..<endIndex]
stringSplits.append(String(theSubString))
startIndex = endIndex
}
return stringSplits
}
}
My requirement is to fit very long string across a few paged views (PDF is not an option, neither is scrolling). Hence I have to figure out string size for each view (same bounds).
No, you don't have to figure that out. The text kit stack will do it for you.
In fact, UITextView will flow long text from one text view to another automatically. It's just a matter of configuring the text kit stack — one layout manager with multiple text containers.

Why is characterRange(at: .zero) == nil?

I have a UITextView with some text that exceeds its bounds. Thus, vertical scrolling is enabled.
Now I want to find the position of the first visible character. This is what I tried:
let firstCharacterPosition = characterRange(at: contentOffset)?.start
This works in most cases. However, when I scroll up and get close to the beginning of the text, the characterRange(at:) function suddenly returns nil.
In the beginning I thought it was only because of bouncing (when the contentOffset.y value shortly becomes < 0). But that's not the (only) reason.
I tried some other values and was surprised to find that
characterRange(at: .zero)
returns nil as well – just as text positions with a low positive y value.
Why is that?
How can I get a reliable UITextPosition for the first visible character?
Please, try this code (remove textView if you are calling it inside UITextView subclass):
let firstVisibleCharacterIndex = textView.layoutManager.characterIndex(for: textView.contentOffset, in: textView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)

Swift 4 UiTextView - delete a word inside textView on button click

Is there a way to delete a specific word inside a UITextView?
let's say for example that in a textView the user wrote: "Hello my nome is john".
As soon as he finished typing he noticed that he mistyped a word.
Lets' say that there is an array initialised with a set o word and "name" is one of this.
when the user go back with the cursor and start deleting the misspelled a list of suggestion comes up.
He detect the word name and click on it.
is there a way to delete the word nome and insert the word name on which he just clicked.
So basically is there a way to get the word immediately before the cursor and remove it from the text view?
I have been able to get the first word before the cursor:
func characterBeforeCursor() -> String? {
// get the cursor position
if let cursorRange = postTextField.selectedTextRange {
// get the position one character before the cursor start position
if let newPosition = postTextField.position(from: cursorRange.start, offset: -1) {
let range = postTextField.textRange(from: newPosition, to: cursorRange.start)
return postTextField.text(in: range!)
}
}
return nil
}
but i wouldn't know how to get the entire word (until the first space, or a particular character e.g "#" ) and delte it from the textview.
I am a bit lost.... so Thanks to anyone will help.

UINavigationItem title truncation

I have text that is built from two Strings:
A(B)
I want to use both strings as the title for the UINavigationItem but in case of truncation I want only A to be truncated and not B.
Example:
The title I want is "The quick brown fox jumps (over the lazy dog)".
The screen size is too small so the title truncates and it is now "The quick brown fox jumps (over...".
What I want it to be is: "The quick br... (over the lazy dog)"
How can I do that?
First check number of characters navigation bar allowing as title without truncating. Next, before setting title check whether the total string length is less than those number of characters or not. Now based on condition set either combined string or B only.
Since xcode won't be able to detect in which part of your title you want to stop and which part you want to start again, you need to do it yourself.
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "Heal the world make it a better place for you and for me and the entire human race"
setNavTitle(title: self.navigationItem.title!)
}
func setNavTitle(title: String)
{
//get the last 10 characters of your title, change the number to your need
let last10 = title.substring(from:title.index(title.endIndex, offsetBy: -14))
//get the first 10 characters of your title, change the number to your need
let first10 = title.substring(to: title.index(title.startIndex, offsetBy: 14))
//concatenate the strings
let title = first10 + " ... " + last10
self.navigationItem.title = title
}

UITextView increment character to the left

I have a UITextView that has a fixed width and height. I pre-populate the entire textfield with blanks.
I would like to insert a character with the push of a button that will erase the last blank character, insert my string character and then place the cursor at the beginning of the newly inserted string. I am trying to achieve inserting special fonts right to left and bottom to top.
It is working with the first button push and on the second button push the new value is inserted in the correct position to the left, however, the cursor will not move to the left after the second button push, it remains to the right after the second string insert.
Here is my code...
-(IBAction)chartP:(id)sender {
NSRange currentRange = myChart.selectedRange;
if (currentRange.length == 0) {
currentRange.location--;
currentRange.length++;
}
myChart.text = [myChart.text stringByReplacingCharactersInRange:currentRange
withString:[NSString string]];
currentRange.length = 0;
myChart.selectedRange = currentRange;
myChart.text = [myChart.text stringByAppendingString:#"p"];
myChart.selectedRange = NSMakeRange(myChart.selectedRange.location -1, 0);
}
Can someone assist me with what I am missing here to continually increment to the left with my string inserts?
How about flipping the text area:
myChart.transform = CGAffineTransformMakeScale(-1,-1);
It sounds like you are trying to implement right-to-left text direction by faking it. Why not do the real thing?
Here's a question that covers the topic:
Change the UITextView Text Direction
If you need bottom-to-top entry, and you have the ability to use a custom font, perhaps you can apply a transformation to the UITextView and y-flip it. Look at the transform property of UIView. (Things like the text selection loupe may break, but it's worth a try.)

Resources