objective c - How can I set [NSTextView selectedTextAttributes] on a background window? -


the default value [nstextview selectedtextattributes] unusable in app, because allow user select colors (syntax highlighting) same background color.

i have written math determine suitable color , can use set it:

textview.selectedtextattributes = @{   nsbackgroundcolorattributename: [nscolor yellowcolor],   nsforegroundcolorattributename: [nscolor redcolor]   }; 

but when window in background, still uses system default light grey.

i've attached screenshots of above code active vs inactive window. — how can change selected text background colour of inactive window?

active inactive

you can override colour overriding drawing method of nslayoutmanager.

final class layoutmanager1: nslayoutmanager {     override func fillbackgroundrectarray(rectarray: unsafepointer<nsrect>, count rectcount: int, forcharacterrange charrange: nsrange, color: nscolor) {         let color1 = color == nscolor.secondaryselectedcontrolcolor() ? nscolor.redcolor() : color         color1.setfill()         super.fillbackgroundrectarray(rectarray, count: rectcount, forcharacterrange: charrange, color: color1)         color.setfill()     } } 

and replace nstextview's layout manager it.

textview.textcontainer!.replacelayoutmanager(layoutmanager1) 

here's full working example.


as @kyle asks reason of setfill, add update.

from apple manual:

... charrange , color parameters passed in merely informational purposes; color set in graphics state. if reason modify it, must restore before returning method. ...

which means passing-in other color super call has no effect, , need nscolor.setfill make work super call. also, manual requires set original one.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -