Reputation: 951
I want to make a NSString notification like this: "You must pay 4.00 dollar.".The money has a red color.In Android ,I can use HTML.formhtml do it.But I don't know how to do it in IOS.Who can help me?
Upvotes: 0
Views: 1294
Reputation: 4901
try this code
CATextLayer *aTextLayer_= [[[CATextLayer alloc]init] autorelease];
aTextLayer_.frame = CGRectMake(10, 100, 300, 50);
aTextLayer_.backgroundColor=[UIColor clearColor].CGColor;
aTextLayer_.foregroundColor = [[UIColor redColor] CGColor];
aTextLayer_.alignmentMode=kCAAlignmentCenter;
aTextLayer_.contentsScale = [[UIScreen mainScreen] scale];
aTextLayer_ .wrapped=YES;
[aTextLayer_ setAlignmentMode:kCAAlignmentLeft];
UIFont *smallFont = [UIFont systemFontOfSize:25];
CTFontRef ctSmallFont = CTFontCreateWithName((CFStringRef)smallFont.fontName, smallFont.pointSize, NULL);
UIFont *boldFont = [UIFont systemFontOfSize:25];
CTFontRef ctBoldFont = CTFontCreateWithName((CFStringRef)boldFont.fontName, boldFont.pointSize, NULL);
CGColorRef cgColor = [UIColor greenColor].CGColor;
CGColorRef cgColor1 = [UIColor redColor].CGColor;
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
(id)ctSmallFont, (id)kCTFontAttributeName,
cgColor, (id)kCTForegroundColorAttributeName, nil];
NSDictionary *subattributes = [NSDictionary dictionaryWithObjectsAndKeys:
(id)ctBoldFont, (id)kCTFontAttributeName,
cgColor1, (id)kCTForegroundColorAttributeName, nil];
CFRelease(ctBoldFont);
NSString *subString=@"You must pay 4.00 dollar.";
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:subString attributes:attributes];
[attrStr addAttributes:subattributes range:NSMakeRange(13,4)];
aTextLayer_.string=attrStr;
[self.view.layer addSublayer:aTextLayer_];
Upvotes: 2
Reputation: 108169
If you don't want to use a UIWebView
your best options is to use a NSAttributedString
.
A naive implementation without caring too much about corner cases and error checking would be something like
NSString * string = @"You must pay 4.00 dollar";
NSString * pattern = @"You must pay (.+) dollar";
NSRegularExpression * regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:NULL];
NSArray * matches = [regex matchesInString:string options:0 range:NSMakeRange(0, string.length)];
NSRange priceRange = [matches[0] rangeAtIndex:1];
NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithString:string];
[attrString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:priceRange];
Note that I assumed that your patter will always be You must pay (.+) dollar
. In case it's different simply adjust the regex in order to retrieve the NSRange
of the substring that needs to be stylized.
Then you can use the attributed string as a content of anything accepting a NSAttributedString
, for instance UILabel
has an attributedText
property since iOS 6, therefore you could do something like
yourLabel.attributedText = attrString;
Upvotes: 1
Reputation: 240
you can add html code to UIWebView
[_webView loadHTMLString:@"<p>You must pay <font color=\"red\">4.00</font> dollar. </p> " baseURL:nil];
try this code
-----------------Alternate Option----------------------------
If this text format is going to be fixed (You must pay XXXX.XXX dollar.) better Create custom component
Upvotes: 0