Pages

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

0 comments:

Post a Comment