IPAD must-haves. And fun-to-haves.

Brighten your iPad with a colorful cover, stream to your TV, download pictures from your digital camera, and more. There’s already so much you can do with iPad and iPad mini

Apple Wireless Keyboard

The incredibly thin Apple Wireless Keyboard uses Bluetooth technology, which makes it compatible with iPad

Apple unveils iPad mini: ‘Thin as a pencil, light as paper’

iPad inspires creativity and hands-on learning with features you won’t find in any other educational tool

Lightning connector and FaceTime HD camera

Apple announces 4th generation iPad packing an A6X CPU

Pages

Showing posts with label attributedText. Show all posts
Showing posts with label attributedText. Show all posts

Wednesday, March 20, 2013

UITextView attributedText with NSStrokeColorAttributeName problems

Hello there, I want to add stroke to UITextView's text in iOS 6.

As iOS 6 add support attributedText to UILabel, UITextField, UITextView, I create a NSMutableAttributedString and set it to UITextView's attributedText. Code is below:

 

- (void)viewDidLoad {

    [super viewDidLoad];

    NSMutableAttributedString *title;

    title = [[NSMutableAttributedString alloc] initWithString:@"Visual comparison"];

    int length = title.length;

    [title addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Noteworthy-Bold" size:36] range:NSMakeRange(0,length)];

    [title addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,length)];

    [title addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,length)];

    [title addAttribute:NSStrokeWidthAttributeName value:@-3.0 range:NSMakeRange(0,length)];

   

    UITextView *label = [[UITextView alloc] initWithFrame:self.view.bounds];    label.attributedText = title;

    [self.view addSubview:label];

}

 

But it take no effect, UITextView's text is not stroked or filled. Replace UITextView with UILabel or UITextField everything will be right.

 

And if you set NSStrokeWidthAttributeName's value to @3.0 and comment one line like this:

 

- (void)viewDidLoad {

    [super viewDidLoad];

    NSMutableAttributedString *title;

    title = [[NSMutableAttributedString alloc] initWithString:@"Visual comparison"];

    int length = title.length;

    [title addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Noteworthy-Bold" size:36] range:NSMakeRange(0,length)];

    [title addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,length)];

//    [title addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,length)];

    [title addAttribute:NSStrokeWidthAttributeName value:@3.0 range:NSMakeRange(0,length)];

   

    UITextView *label = [[UITextView alloc] initWithFrame:self.view.bounds];    label.attributedText = title;

    [self.view addSubview:label];

}

 

UITextView's text is stroked exactly.

 

If you add these lines:

 

- (void)viewDidLoad {

    [super viewDidLoad];

    NSMutableAttributedString *title;

    title = [[NSMutableAttributedString alloc] initWithString:@"Visual comparison"];

    int length = title.length;

    [title addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Noteworthy-Bold" size:36] range:NSMakeRange(0,length)];

    [title addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,length)];

    if (length % 2 == 0)

    {

        length = length / 2 - 1;

    }

    else

    {

        length = length / 2;

    }

    [title addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,length)];

    [title addAttribute:NSStrokeWidthAttributeName value:@-3.0 range:NSMakeRange(0,length)];

   

    UITextView *label = [[UITextView alloc] initWithFrame:self.view.bounds];    label.attributedText = title;

    [self.view addSubview:label];

}

 

and then UITextView's text will be correct again.

 

Anyone know why?


View the original article here