Re: How to convert String.Index to UInt?
Re: How to convert String.Index to UInt?
- Subject: Re: How to convert String.Index to UInt?
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Sat, 16 Aug 2014 17:48:01 +0700
On 16 Aug 2014, at 16:25, Stephen J. Butler <email@hidden> wrote:
> Try:
>
> import Cocoa
>
> let s = "hallo\there"
> let aas = NSMutableAttributedString(string: s, attributes: nil)
>
> if let rangeOfTab = s.rangeOfString( "\t" ) {
> let colour = NSColor.grayColor()
> let length = distance(s.startIndex, rangeOfTab.startIndex)
> let aRange = NSRange(location: 0, length: length)
> aas.addAttribute(NSForegroundColorAttributeName, value: colour, range: aRange)
> }
Did try:
let s = "a 🚲 bicycle"
let endGray = "c"
if let rangeOfB = s.rangeOfString(endGray )
{
let length = distance(s.startIndex, rangeOfB.startIndex)
println( "\(endGray) at composed character index index \(length)") // 6
}
let ns = s as NSString
let range = ns.rangeOfString(endGray)
let nsLength = range.location
println( "\(endGray) at NSString index \(nsLength)") // 7
Using "length" with addAttribute:value:range would NOT be quite correct, would it?
Seems like "nsLength" is more appropriate.
Kind regards,
Gerriet.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden