Re: Swift: How to determine if a Character represents whitespace?

2015-04-03 Thread Roland King
ok here’s my try, assuming NSLinguisticTagger knows what it’s doing. And yes 
it’s a bit stupid to use a linguistic tagger to do something like that but .. 
whatever 

var str = "Some String WIth Whitespace "

var lt = NSLinguisticTagger( tagSchemes: [NSLinguisticTagSchemeTokenType], 
options: 0 )
lt.string = str

var endsWithWhitespace = ( lt.tagAtIndex( (str as NSString).length-1, scheme: 
NSLinguisticTagSchemeTokenType, tokenRange: nil, sentenceRange: nil ) == 
NSLinguisticTagOtherWhitespace )


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Converting colorspace of a CGColor

2015-04-03 Thread Graham Cox

> On 4 Apr 2015, at 1:13 pm, Roland King  wrote:
> 
> That’s most definitely not what Graham is looking for. He’s looking for a way 
> to convert colours programatically, and cross-platform iOS and OSX (no 
> colorsync on iOS). 


That's right. In short, a CG equivalent to -[NSColor colorUsingColorspace:] is 
what I need - I just need to ensure that colours that could originate as grays 
for example, end up as RGBA colours, since I'm doing work directly on the four 
components. Converting gray is easy enough actually, if I have to do that 
myself, but it would be nice if there were a funciton that would work across 
all possible colorspaces.

I thought Id' seen a CG function for this in the past, but I can't remember 
where I saw it and I may be mistaken.

--Graham



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Converting colorspace of a CGColor

2015-04-03 Thread Roland King

> On 4 Apr 2015, at 9:38 am, edward m taffel  wrote:
> 
> use ColorSync.
> 

That’s most definitely not what Graham is looking for. He’s looking for a way 
to convert colours programatically, and cross-platform iOS and OSX (no 
colorsync on iOS). 

I just looked in my library of bits and pieces and found some code which write 
into a bitmap and reads it out again. I don’t even know when I wrote that, and 
it’s not used in any of my projects, but I believe that was me converting 
colours at low-level. 

Surely there’s a better way (but then again I said ‘surely there is a better 
way’ when I saw the discussion on whitespace in swift and there hasn’t been one 
yet). 

> Sent from my iPhone
> 
>> On Apr 3, 2015, at 8:35 PM, Graham Cox  wrote:
>> 
>> Is there a way to convert a CGColor to a different colorspace? It's trivial 
>> with NSColor, but I want to do the same at the lower CG level to keep the 
>> code as portable as possible between iOS and Mac. I'm sure I've done this in 
>> the past but I'm just not seeing the right function!
>> 
>> --Graham


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Converting colorspace of a CGColor

2015-04-03 Thread edward m taffel
use ColorSync.

Sent from my iPhone

> On Apr 3, 2015, at 8:35 PM, Graham Cox  wrote:
> 
> Is there a way to convert a CGColor to a different colorspace? It's trivial 
> with NSColor, but I want to do the same at the lower CG level to keep the 
> code as portable as possible between iOS and Mac. I'm sure I've done this in 
> the past but I'm just not seeing the right function!
> 
> --Graham
> 
> 
> 
> ___
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/etaffel%40me.com
> 
> This email sent to etaf...@me.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Converting colorspace of a CGColor

2015-04-03 Thread Graham Cox
Is there a way to convert a CGColor to a different colorspace? It's trivial 
with NSColor, but I want to do the same at the lower CG level to keep the code 
as portable as possible between iOS and Mac. I'm sure I've done this in the 
past but I'm just not seeing the right function!

--Graham



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Swift: How to determine if a Character represents whitespace?

2015-04-03 Thread Quincey Morris
On Apr 3, 2015, at 13:18 , Charles Jenkins  wrote:
> 
> is there a way in the playground for use to test addresses to make sure 
> attrStr.string as NSString doesn’t perform a copy? 

I doubt it. This is the best I could come up with in a couple of minutes:

> import Cocoa
> 
> let notWhitespace = 
> NSCharacterSet.whitespaceAndNewlineCharacterSet().invertedSet
> let count = 50
> 
> let aString: String = String (count: count, repeatedValue: Character ("A"))
> let aNSString: NSString = ("" as NSString).stringByPaddingToLength (count, 
> withString: "A", startingAtIndex: 0)
> 
> let date1 = NSDate ()
> let bString: String = aNSString as String
> let date2 = NSDate ()
> let time2 = date2.timeIntervalSinceDate(date1)
> 
> let date3 = NSDate ()
> let bNSString: NSString = aString as NSString
> let date4 = NSDate ()
> let time4 = date4.timeIntervalSinceDate(date3)
> 
> let attrStr = NSAttributedString (string: aNSString)
> 
> let date5 = NSDate ()
> let range5 = attrStr.string.rangeOfCharacterFromSet(notWhitespace, options: 
> NSStringCompareOptions.allZeros)
> let date6 = NSDate ()
> let time6 = date6.timeIntervalSinceDate(date5)
> 
> let date7 = NSDate ()
> let range7 = (attrStr.string as 
> NSString).rangeOfCharacterFromSet(notWhitespace, options: 
> NSStringCompareOptions.allZeros)
> let date8 = NSDate ()
> let time8 = date8.timeIntervalSinceDate(date7)

Playground results:

time2: 0.3328
time4: 0.1817
time6: 0.0022
time8: 0.0017

Since the “rangeOfCharacter” scans terminate at the first character, this 
suggests that there’s no real conversion in the last case, which is the one 
you’re interested in. (Also, time6 and time8 don’t vary with the value of 
‘count’.) However, generalizing from this seems treacherous. And I may have 
just Done It Wrong™.



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Swift: How to determine if a Character represents whitespace?

2015-04-03 Thread Charles Jenkins
So my Character.isMemberOfSet() is a poor general-purpose method, and I need to 
ditch it.

I like your code. I had to modify it a bit so it wouldn’t fall on strings 
composed entirely of whitespace:

let testString = "     \n\n \t\t"
let attrStr = NSAttributedString( string:testString )
let str = attrStr.string as NSString

let notWhitespace = 
NSCharacterSet.whitespaceAndNewlineCharacterSet().invertedSet

var resultRange:NSRange

let startRange = str.rangeOfCharacterFromSet( notWhitespace, 
options:NSStringCompareOptions.allZeros )
if startRange.length > 0 {
  let endRange = str.rangeOfCharacterFromSet( notWhitespace, 
options:NSStringCompareOptions.BackwardsSearch )
  let startIndex = startRange.location
  let endIndex = endRange.location + endRange.length
  resultRange = NSRange( location:startIndex, length:endIndex - startIndex )
} else {
  // String is empty or all whitespace
  resultRange = NSRange( location:0, length:0 )
}
let resultStr = attrStr.attributedSubstringFromRange( resultRange )
 
So, even though attrStr.string returns an NSString, you have use the “as” to 
explicitly keep the type and be able to do math on range indexes. Lacking that 
cast is what made rangeOfCharacterFromSet() useless to me yesterday.

Your code seems way better. but is there a way in the playground for use to 
test addresses to make sure attrStr.string as NSString doesn’t perform a copy? 

— 

Charles

On April 3, 2015 at 2:04:01 PM, Quincey Morris 
(quinceymor...@rivergatesoftware.com) wrote:

On Apr 3, 2015, at 04:00 , Charles Jenkins  wrote:

    for char in String( self ).utf16 {
      if set.characterIsMember( char ) {
        return true
      }

Now we’re back to the place we started. This code is wrong. It fails for any 
code point that isn’t representable a single UTF-16 code value, and it fails 
for any grapheme that isn’t representable as a single code point.

This is what I would do (playground version):

import Cocoa

let notWhitespace = 
NSCharacterSet.whitespaceAndNewlineCharacterSet().invertedSet
let attrStr = NSAttributedString( string:"    Fourscore and seven years ago... 
\n\n \t\t" )
let str = attrStr.string as NSString

let startRange = str.rangeOfCharacterFromSet(notWhitespace, options: 
NSStringCompareOptions.allZeros)
let endRange = str.rangeOfCharacterFromSet(notWhitespace, options: 
NSStringCompareOptions.BackwardsSearch)

let startIndex = startRange.length != 0 ? startRange.location : 0
let endIndex = endRange.length != 0 ? endRange.location + 1 : str.length

let resultRange = NSRange (location: startIndex, length: endIndex - startIndex)
let resultStr = attrStr.attributedSubstringFromRange (resultRange)

It’s the Obj-C code, just written in Swift. The ‘as NSString’ in the 3rd line 
makes it work.

The practical difficulty in your original approach is that (e.g.) 
String.rangeOfCharacterFromSet returns a Range, but AFAICT that 
isn’t convertible back to a NSRange, or even just integer indexes. At the same 
time, AFAICT it isn’t useful with a String because it doesn’t contain Character 
indexes, just unichar indexes, which have no meaning for a String in general.

Actually, my testing is with Swift 1.1, since I’m not in a position to move to 
Xcode 6.3 yet. It’s possible that the results are different in Swift 1.2. 
However, in the section of the release notes that talks about bridging between 
String and NSString, it says:

Note that these Cocoa types in Objective-C headers are still automatically 
bridged to their corresponding Swift type

so I suspect the results would be the same in 1.2. It seems to me there is an 
actual bug here:

“String methods corresponding to NSString methods that return NSRange values 
actually return Range values, but these are not valid ranges, 
either for String objects (they represent UTF-16 code value positions, not 
Character positions) or for NSString objects (they’re not convertible back to 
NSRange). The String methods ought to return NSRange values just like their 
NSString counterparts.”

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Swift: How to determine if a Character represents whitespace?

2015-04-03 Thread Quincey Morris
On Apr 3, 2015, at 11:19 , Marco S Hyman  wrote:
> 
> The original code will return true only if all code points map to white space.

The “failure” I was talking about is something a bit different. It has two 
problems:

1. For Unicode code points that are represented by 2 code values, it tests the 
code values, not the code points. That’s wrong.

2. For graphemes that are represented by 2 or more code points, it still tests 
the code values, of which there could be 4 or more per grapheme. That’s also 
wrong. With the ‘for char in String (self)’ code, if you tested whether a 
decomposed acuteA was in the (7-bit) ASCII character set, you’d get the answer 
“YES".

You could mitigate #1 by using UTF-32 code values instead of UTF-16, but that 
wouldn’t help with #2.



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Swift: How to determine if a Character represents whitespace?

2015-04-03 Thread Marco S Hyman

> On Apr 3, 2015, at 11:04 AM, Quincey Morris 
>  wrote:
> 
> On Apr 3, 2015, at 04:00 , Charles Jenkins  wrote:
>> 
>>for char in String( self ).utf16 {
>>  if set.characterIsMember( char ) {
>>return true
>>  }
> 
> Now we’re back to the place we started. This code is wrong. It fails for any 
> code point that isn’t representable a single UTF-16 code value, and it fails 
> for any grapheme that isn’t representable as a single code point.

No it doesn't.   Give it a test.

let acuteA: Character = "\u{e1}"   // An "a" with an accent
let acuteAComposed: Character = "\u{61}\u{301}"// Also an "a" with an accent

func howManyChars( c: Character) -> Int {
var count = 0
for char in String( c ).utf16 {
count += 1
}
return count
}

howManyChars(acuteA)// returns 1
howManyChars(acuteAComposed)// returns 2

The original code will return true only if all code points map to white space.

Marc

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Swift: How to determine if a Character represents whitespace?

2015-04-03 Thread Quincey Morris
On Apr 3, 2015, at 04:00 , Charles Jenkins  wrote:
> 
> for char in String( self ).utf16 {
>   if set.characterIsMember( char ) {
> return true
>   }

Now we’re back to the place we started. This code is wrong. It fails for any 
code point that isn’t representable a single UTF-16 code value, and it fails 
for any grapheme that isn’t representable as a single code point.

This is what I would do (playground version):

> import Cocoa
> 
> let notWhitespace = 
> NSCharacterSet.whitespaceAndNewlineCharacterSet().invertedSet
> let attrStr = NSAttributedString( string:"Fourscore and seven years 
> ago... \n\n \t\t" )
> let str = attrStr.string as NSString
> 
> let startRange = str.rangeOfCharacterFromSet(notWhitespace, options: 
> NSStringCompareOptions.allZeros)
> let endRange = str.rangeOfCharacterFromSet(notWhitespace, options: 
> NSStringCompareOptions.BackwardsSearch)
> 
> let startIndex = startRange.length != 0 ? startRange.location : 0
> let endIndex = endRange.length != 0 ? endRange.location + 1 : str.length
> 
> let resultRange = NSRange (location: startIndex, length: endIndex - 
> startIndex)
> let resultStr = attrStr.attributedSubstringFromRange (resultRange)

It’s the Obj-C code, just written in Swift. The ‘as NSString’ in the 3rd line 
makes it work.

The practical difficulty in your original approach is that (e.g.) 
String.rangeOfCharacterFromSet returns a Range, but AFAICT that 
isn’t convertible back to a NSRange, or even just integer indexes. At the same 
time, AFAICT it isn’t useful with a String because it doesn’t contain Character 
indexes, just unichar indexes, which have no meaning for a String in general.

Actually, my testing is with Swift 1.1, since I’m not in a position to move to 
Xcode 6.3 yet. It’s possible that the results are different in Swift 1.2. 
However, in the section of the release notes that talks about bridging between 
String and NSString, it says:

> Note that these Cocoa types in Objective-C headers are still automatically 
> bridged to their corresponding Swift type

so I suspect the results would be the same in 1.2. It seems to me there is an 
actual bug here:

“String methods corresponding to NSString methods that return NSRange values 
actually return Range values, but these are not valid ranges, 
either for String objects (they represent UTF-16 code value positions, not 
Character positions) or for NSString objects (they’re not convertible back to 
NSRange). The String methods ought to return NSRange values just like their 
NSString counterparts.”



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Swift: How to determine if a Character represents whitespace?

2015-04-03 Thread Marco S Hyman

> extension Character {
> 
>   func isMemberOfSet( set:NSCharacterSet )
> -> Bool
>   {
> // The for loop only executes once;
> // its purpose is to convert Character to a type
> // you can actually do something with
> for char in String( self ).utf16 {
>   if set.characterIsMember( char ) {
> return true
>   }
> }
> return false
>   }
> 
> }

I believe your comment that the loop executes once is incorrect.   It may
execute more than once when the Character is a composed character that
maps to multiple utf16 characters.

Example (stolen from this link):
  
http://stackoverflow.com/questions/27697508/nscharacterset-characterismember-with-swifts-character-type


let acuteA: Character = "\u{e1}"   // An "a" with an accent
let acuteAComposed: Character = "\u{61}\u{301}"// Also an "a" with an accent

Both are a single Character.  The latter will cause the loop to iterate twice.

Marc
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Swift: How to determine if a Character represents whitespace?

2015-04-03 Thread Charles Jenkins
I imagine you’re right, that they’re NString indexes packaged up into a 
frustrating return type. After sleeping on it, though, I imagined that even if 
complex grapheme clusters WERE to make count( attrStr.string ) return a 
different result than attrStr.length, it would probably never be due to 
whitespace. So if I go back to Charles Strstka’s original suggestion, where you 
pull off one character at a time, convert it to a 1-Character string, and then 
test for whitespace membership, I should be able to count leading and trailing 
whitespace characters and then do math based on attrStr.length to create the 
range.

Here’s my current playground: 

import Cocoa

extension Character {

  func isMemberOfSet( set:NSCharacterSet )
    -> Bool
  {
    // The for loop only executes once;
    // its purpose is to convert Character to a type
    // you can actually do something with
    for char in String( self ).utf16 {
      if set.characterIsMember( char ) {
        return true
      }
    }
    return false
  }

}

var result:NSRange

let whitespace = NSCharacterSet.whitespaceAndNewlineCharacterSet()

let attrStr = NSAttributedString( string:"    Fourscore and seven years ago... 
\n\n \t\t" )
let str = attrStr.string

var headCount = 0
var tailCount = 0

var startIx = str.startIndex
var endIx = str.endIndex

while endIx > startIx && str[ endIx.predecessor() ].isMemberOfSet( whitespace ) 
{
  ++tailCount
  endIx = endIx.predecessor()
}
if endIx > startIx {
  while str[ startIx ].isMemberOfSet( whitespace ) {
    ++headCount
    startIx = startIx.successor()
  }
  let length = attrStr.length - ( headCount + tailCount )
  result = NSRange( location:headCount, length:length )
} else {
  // String was empty or all whitespace
  result = NSRange( location:0, length:0 )
}

let resultString = attrStr.attributedSubstringFromRange( result )


— 

Charles

On April 2, 2015 at 11:16:52 PM, Quincey Morris 
(quinceymor...@rivergatesoftware.com) wrote:

On Apr 2, 2015, at 19:28 , Charles Jenkins  wrote:

I can indeed call attrStr.string.rangeOfCharacterFromSet(). But in typical 
Swift string fashion, the return type is as unfriendly as possible: 
Range? — as if the NSString were a Swift string.

I finally read the whole of what you said here, and I had to run to a 
playground to check:

import Cocoa

var strA = "Hello?, String”
var strB = "Hello?, String" as NSString
var strC = "Hello\u{1f650}, String”
var strD = "Hello\u{1f650}, NSString" as NSString
var rangeA = 
strA.rangeOfCharacterFromSet(NSCharacterSet.whitespaceCharacterSet()) // {Some 
“7..<8”}
var rangeB = 
strB.rangeOfCharacterFromSet(NSCharacterSet.whitespaceCharacterSet()) // (7,1)
var rangeC = 
strC.rangeOfCharacterFromSet(NSCharacterSet.whitespaceCharacterSet()) // {Some 
“8..<9”}
var rangeD = 
strD.rangeOfCharacterFromSet(NSCharacterSet.whitespaceCharacterSet()) // (8,1)

So, yes, these are NSString indexes all the way, even if the result is packaged 
as a Range.

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Do NSStackView's actually work in a NSTableView?

2015-04-03 Thread Jonathan Mitchell
Obviously I am not sure whether you are introducing the NSStackView in to the 
table view in in IB or in code.
Have you made sure that the NSStackView is fully constrained within the 
NSTableCellView?

IB by default does not add constraints to NSView child items added to 
NSTableCellView instances.

> On 3 Apr 2015, at 04:52, Sebastien Boisvert  
> wrote:
> 
> I only ask because I’m using a stackview that while it behaves as I 
> configured it in a plain view in a window, it behaves completely different 
> when put in a tableview. For example, views which I’ve set to detach will 
> never, ever detach, regardless of the settings I’ve set.
> 
> Anyone have experience with using stackviews in tableviews?
> ___
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/jonathan%40mugginsoft.com
> 
> This email sent to jonat...@mugginsoft.com


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com