[英]Bolding a string using NSMutableAttributedString
Im using the following code to bold a part of string. 我使用以下代码将字符串的一部分加粗。 I want only the Shipment Ref. #:
我只需要Shipment Ref. #:
Shipment Ref. #:
to be bolded. Shipment Ref. #:
要加粗。 Following is my code 以下是我的代码
UIFont *boldFont = [UIFont boldSystemFontOfSize:12];
NSString *yourString = [NSString stringWithFormat:@"Shipment Ref. #: %@ ",[[[NSUserDefaults alloc] init] valueForKey:@"shipRef"]];
NSRange boldedRange = NSMakeRange(10, 4);
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString];
[attrString beginEditing];
[attrString addAttribute:NSFontAttributeName
value:boldFont
range:boldedRange];
[attrString endEditing];
self.shipRefHeader.text = [attrString mutableString];
The problem is that it is not getting bolded 问题是它没有被加粗
try this code 试试这个代码
NSString * yourString = [NSString stringWithFormat:@"Shipment Ref. #: %@ ",[[[NSUserDefaults alloc] init] valueForKey:@"shipRef"]];;
NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithString:yourString];
NSString *boldString = @"Shipment Ref. #:";
NSRange boldRange = [yourString rangeOfString:boldString];
[attrString addAttribute: NSFontAttributeName value:[UIFont boldSystemFontOfSize:12] range:boldRange];
[self.shipRefHeader setAttributedText: attrString];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.