Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the range of string for attributed string?

I have string as 2/42. Here 2 & 42 is dynamic.Now I want to set /42 as superscript of string. I am not able to find out the range of /42. please tell me how do i find out the range in swift?

like image 805
TechChain Avatar asked Dec 18 '22 02:12

TechChain


1 Answers

Mutable string of attributed string has functionality to find of range of specific string.

Try this and see (Swift 4):

let stringValue = "2/42"
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: stringValue)
let range: NSRange = attributedString.mutableString.range(of: "/42", options: .caseInsensitive)
print("range - \(range)")

Result:
range - {1, 3}

Ref Snapshot:

enter image description here

like image 182
Krunal Avatar answered Jan 06 '23 02:01

Krunal