iPhone CoreText: Find the pixel coordinates of a substring

后端 未结 4 1668
故里飘歌
故里飘歌 2021-01-30 19:09

Here\'s a screenshot of the twitter app for reference: http://screencast.com/t/YmFmYmI4M

What I want to do is place a floating pop-over on top of a substring in an NSAtt

相关标签:
4条回答
  • 2021-01-30 19:30

    The link given by George was very helpful and got me what I wanted. But strange thing happened. It was working in iOS SDK 4.0 but in the iOS SDK 5 the position of the link appeared in wrong position on the view.

    So I had to tweak the code a little bit. For the x coordinates of the touchable button, I had to use CTRunGetTypographicBounds instead of the CTRunGetImageBounds function.

    So over all, in the tweaked code: The y Coordinate , width and height was calculated using CTRunGetImageBounds. And x coordinate was calculated using CTRunGetTypographicBounds.

    0 讨论(0)
  • 2021-01-30 19:35

    I've been working on a small library that does exactly that. You can find it here: https://github.com/pothibo/CMFramework

    However, this library is in its alpha stage, there's optimization needed and some feature are lacking (line height is one of the urgent feature I want to add)

    If you decide to use it and find issue, don't hesitate to post issues on github!

    0 讨论(0)
  • 2021-01-30 19:45

    you need to find Y by CTLine and X by CTRun width and height you can get by word and font itself. ill attache my project link, its really simple code but you can reedit in order to meet your needs. hope it helps cheers if you improve general logic please let me know thx. textViewProject

    0 讨论(0)
  • 2021-01-30 19:47

    Set some attribute in the attributed string that won't effect display, but will cause it to be laid out as a seperate glyph run, then use CoreText to layout the string

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
    CTFrameRef ctframe = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
    

    Now you will have to hunt through the frame to find the relevant chunk of text. Get the array of CTLineRefs with CTFrameGetLines().

    Iterate through the array, testing if the touch was on that line, by checking it is within the rect returned by CTLineGetImageBounds(). If it is, now look through the glyph runs in the line.

    Again, you can get an array of CTRunRefs with CTLineGetGlyphRuns(). Check whether the tap was within the glyph run with CTRunGetImageBounds(), and if it was you can find the the range of indices in the original attributed string that the glyph run corresponds to with CTRunGetStringIndices().

    0 讨论(0)
提交回复
热议问题