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?

iOS 6

Posted on Mar 8, 2013 7:48 PM

Reply
2 replies

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

UITextView attributedText with NSStrokeColorAttributeName problems

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.