Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two different text color not working #790

Open
ChiragKothiya1996 opened this issue Aug 16, 2018 · 0 comments
Open

Two different text color not working #790

ChiragKothiya1996 opened this issue Aug 16, 2018 · 0 comments

Comments

@ChiragKothiya1996
Copy link

ChiragKothiya1996 commented Aug 16, 2018

In label set text like "text with #tag and www.example.com".

my need is set red color for "www.example.com" and green color for "#tag".

But it set blue color.

Following is my code:

[label setText:@"text with #tag and www.example.com" afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
	NSRange rangeUrl = [[mutableAttributedString string] rangeOfString:@"www.example.com" options:NSCaseInsensitiveSearch];
    NSRange rangeTag = [[mutableAttributedString string] rangeOfString:@"#tag" options:NSCaseInsensitiveSearch];
    UIFont *boldSystemFont = [UIFont systemFontOfSize:IS_IPAD?16:14 weight:UIFontWeightRegular];
    CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
    if (font) {

		[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:rangeUrl];
		[mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:[UIColor redColor] range:rangeUrl];

		[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:rangeTag];
  		[mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:[UIColor greenColor] range: rangeTag];
		[mutableAttributedString addAttribute:(NSString *)kCTUnderlineColorAttributeName value:[UIColor clearColor] range: rangeTag];

		CFRelease(font);
	}
	return mutableAttributedString;
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant