iphone - How to highlight CoreText with changing highlight colors? -


i have subclass of uiview implements code draw coretext. in application, uiview drawn inside uiscrollview. here code use in drawrect: render nsattributedstring:

cgcontextref context = uigraphicsgetcurrentcontext();  float viewheight = self.bounds.size.height; cgcontexttranslatectm(context, 0, viewheight); cgcontextscalectm(context, 1.0, -1.0); cgcontextsettextmatrix(context, cgaffinetransformmakescale(1.0, 1.0));  cgmutablepathref path = cgpathcreatemutable(); cgrect bounds = cgrectmake(padding_left, -padding_top, self.bounds.size.width-20.0, self.bounds.size.height); cgpathaddrect(path, null, bounds);  ctframesetterref framesetter = ctframesettercreatewithattributedstring((cfmutableattributedstringref)attrstring); ctframeref frame = ctframesettercreateframe(framesetter, cfrangemake(0, 0), path, null); cfrelease(framesetter); cfrelease(path); ctframedraw(frame, context); 

in nsattributedstring, have text attributes color text.

what need conceptually, how should accomplished? confused various methods of drawing frames, layers, etc. have tried review several books , other resources, feel i'm missing basic stuff.

here requirements:

  1. highlights need redrawn new location on change of orientation.
  2. highlight color needs able changed programmatically on individual highlight basis.
  3. ideally performance, don't want have re-draw entire coretext uiview when change highlight color of 1 highlight.
  4. the individual rects must able grouped form single highlight since highlights can cover range goes beyond single ctline.
  5. eventually, want implement uimenucontroller on coretext view believe highlights must drawn underneath coretext uiview?

i appreciate assistance.

here have tried far:

i added new uiview subview uiscrollview contain highlights. add individual uiviews highlights themselves. when create dummy "test" highlights, appear correctly under coretext view. can change highlight color. however, when go acquire actual information lines coretext view create real highlights, returns 0 lines. because drawrect isn't finished drawing yet or else?

here questions:

  1. should add uiview subview uiscrollview contain highlights additional subviews each individual highlight? then, store each highlight uiview in nested nsarray?
  2. if change color of highlight beneath coretext uiview, cause entire uiview redrawn? if so, how can design around this?
  3. it true if want coretext uiview receive input uimenucontroller, have place highlight objects beneath coretext uiview?

i have seen open source project:

https://github.com/cocoanetics/nsattributedstring-additions-for-html

this amazing can do, far more complex , detailed need. code renders each individual glyph run using coretext , permits highlighting, awesome. however, trying keep code simple , lightweight. know little coregraphics, coreanimation or coretext.

i have seen question:

core text - pixel coordinates nsrange

the answer question provides calls determine bounds of rect given range in given line. think can figure out calls.

update

here have done now. i'm sure wrong, way find make work...

  1. create uiview container add uiscrollview subview.
  2. pass uiview coretextview. pass array of string ranges coretextview indicate strings should highlighted.
  3. add coretextview subview of uiscrollview. way, uiview underneath coretextview.
  4. in drawrect: function of coretextview, draw coretext. call function calculate highlights.
  5. each nsrange checked against lines , cgrect of each match in line calculated.
  6. cgrect used create new uiview. uiview added subview of uiview container. added nsarray can keep track of , change highlight color of individual highlight needed.

this not call drawrect each time highlight added container or color changed. however, know isn't ideal solution. problems solution:

  1. when uiviewcontroller calls setneedsdisplay on coretextview, there no way know when done drawing. still can't find way callback tell viewcontroller okay proceed next task.
  2. by adding uiviews other container uiview, drawrect technically drawing items outside of own rect, know no no.
  3. i'm sure isn't performant way of doing this, although seem work pretty well.

your basic idea isn't bad highlighting. don't know if it's easier highlighting glyphs while draw, it's not bad idea, , makes easy turn on , off highlighting, that's pretty nice.

rather creating lots of views, put core text onto calayer instead. can create additional highlighting layers put on text. keeps inside of single view. layers cheaper views. if you're trying "highlighter" style highlighting, can set backgroundcolor , frame layers , you're done.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -