0

I would like to know that how can i set Letter spacing in iOS 6? It's working fine for iOS 7 using below code, now need to do for iOS 6 :

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[arraySettings objectAtIndex:indexPath.row]];

float spacing = 0.2f;
[attributedString addAttribute:NSKernAttributeName
                                 value:@(spacing)
                                 range:NSMakeRange(0, [[arraySettings objectAtIndex:indexPath.row] length])];

cell.textLabel.attributedText = attributedString;

error image after crashing above code in iOS 6 :

enter image description here

Thanks.

Anand Gautam
  • 2,541
  • 3
  • 34
  • 70
  • Have you tried using a bigger value? The default value is 0 and 0.2 would be quite hard to see a difference visually. The above code should work fine in iOS 6 and `NSKernAttributeName` was first introduced then. – liamnichols Jun 30 '14 at 09:39
  • @liamnicholsThanks, my values are coming from server..i have tried with bigger value but my above code is crashing in iOS 6 and its working fine in iOS7. Any other way to solve this problem. – Anand Gautam Jun 30 '14 at 09:45
  • Oh, it's crashing? Could you update your question with information about the exception that is being raised? – liamnichols Jun 30 '14 at 09:46
  • Was any related information printed into the debugger? – liamnichols Jun 30 '14 at 09:51
  • No.. i am not getting any error message. – Anand Gautam Jun 30 '14 at 09:54

2 Answers2

0

I had to do same thing in one of my project and used this same code. It was working fine for me.

NSMutableAttributedString *attributedString =
[[NSMutableAttributedString alloc] initWithString:@"hi this is my testing string"];    
float spacing = 1.0f;
[attributedString addAttribute:NSKernAttributeName
                         value:@(spacing)
                         range:NSMakeRange(0, [@"hi this is my testing string" length])];    
mylbl.attributedText = attributedString;
holex
  • 23,961
  • 7
  • 62
  • 76
sam
  • 117
  • 10
  • It is crashing, i have checked in iOS6. – Anand Gautam Jun 30 '14 at 09:47
  • What it says while crashing. I mean what is your crash log?https://www.dropbox.com/s/vwdbhxefv1ewf0m/lblTest.zip. This is demo app I just created with this code and it's working both in iOS6 and iOS7. – sam Jun 30 '14 at 10:19
  • Use this code and let me know if it's working or not. May be problem in your array object – sam Jun 30 '14 at 10:20
0

My guess is that you have some sort of memory related issue with your arraySettings ivar.

I've slightly modified the code you provided in a test project and it seemed to work fine for me when run on both the iOS 6.1 and 7.1 simulator.

Could you try using the following code and see what happens?

NSString *string = [[arraySettings objectAtIndex:indexPath.row] copy];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];

CGFloat spacing = 0.2f;
[attributedString addAttribute:NSKernAttributeName value:@(spacing) range:NSMakeRange(0, string.length)];

cell.textLabel.attributedText = attributedString;

If you are still getting a crash then you will need to provide more info about what you are doing with the arraySettings object.

liamnichols
  • 12,419
  • 2
  • 43
  • 62
  • Thanks.. but again it's crashing on line-4 without giving any error message.here is my array initialization. arraySettings = [[NSMutableArray alloc] initWithObjects:@"USER GUIDE", @"PROFILE PHOTO", @"SYNC DATA", @"HELP", @"LOGOUT", nil];..pls check in your code using same array objects. – Anand Gautam Jun 30 '14 at 10:09
  • Where are you calling the above code? In the `tableView:cellForRowAtIndexPath:` method? Are there any warnings being generated at compile time about any related code? With the limited information provided there isn't much anybody can do to help you out here. – liamnichols Jun 30 '14 at 10:19