2016-12-16 11 views
0

次のコードを使用して、NSTextViewの前景色を取得しようとしています。残念ながら、私は関係する色空間と関連している必要がありますランタイムエラーを取得します。NSTextViewの前景色を取得できません

[General] *** invalid number of components for colorspace in initWithColorSpace:components:count:

答えて

0
私はそれが次のように働いて作られた

let components = UnsafeMutablePointer<CGFloat>.allocate(capacity: 4) 
rowObj.foregroundColor!.getComponents(components) 
let c = NSColor(colorSpace: NSColorSpace.sRGB, components: components, count: 4) 
components.deinitialize() 
components.deallocate(capacity: 4) 
let r = c.redComponent 
let g = c.greenComponent 
let b = c.blueComponent 

編集:私はメモリ上の修正をした私は、次のランタイムエラーを取得

if let textStorage = textView.textStorage { 
    let rowObj = textStorage.paragraphs[row] 
    let range = NSMakeRange(0, rowObj.string.characters.count) 
    colorOrRowBeforeSelection = rowObj.foregroundColor!     
    if(rowObj.foregroundColor != nil) { 
     let r = rowObj.foregroundColor!.redComponent 
     let g = rowObj.foregroundColor!.greenComponent 
     let b = rowObj.foregroundColor!.blueComponent 
    } else { 
     Log.e("cannot get foreground color components") 
    } 
} else { 
    Log.e("textStorage = nil") 
}  

のmはどのように修正することができます現在、コードは白以外のもの(白でなければなりません)を除いて動作します(グレーの色空間)黄色に...誰かがそれを修正してくれることを願っています。

関連する問題