I need to render rich text using Core Text in my view (simple formatting, multiple fonts in one line of texts, etc.). I am wondering if text rendered this way can be selected by user using (standard copy / paste function)?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I implemented a text selection in CoreText. It is really a hard work… But it’s doable.
Basically you have to save all
CTLinerects and origins usingCTFrameGetLineOrigins(1),CTLineGetTypographicBounds(2),CTLineGetStringRange(3) andCTLineGetOffsetForStringIndex(4).The line rect can be calculated using the origin(1), ascent(2), descent(2) and offset(3)(4) as shown bellow.
After doing that, you can test which line has the touched point looping the lines (always remember that CoreText uses inverse Y coordinates).
Knowing the line that has the touched point, you can know the letter that is located at that point (or the nearest letter) using
CTLineGetStringIndexForPosition.Here’s one screenshot.
For that loupe, I used the code shown in this post.
Edit:
To draw the blue background selection, you have to paint the rect using
CGContextFillRect. Unfortunately, there’s no background color inNSAttributedString.