简体   繁体   English

使用NSMutableAttributedString粗体显示字符串

[英]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.

相关问题 使用NSMutableAttributedString为字符串中的单词添加颜色 - Adding color to a word in string using NSMutableAttributedString 在 iOS 8 中使用 NSMutableAttributedString 的字符串的下划线部分不起作用 - Underline part of a string using NSMutableAttributedString in iOS 8 is not working 如何替换NSMutableAttributedString中的字符串? - How to replace string in NSMutableAttributedString? 在iOS中使用NSMutableAttributedString作为常量 - Using NSMutableAttributedString as a Constant in iOS 动画NSMutableAttributedString颜色-字符串消失 - Animating NSMutableAttributedString color - the string disappears 将字符串添加到NSMutableAttributedString时出错 - Error when adding a string to a NSMutableAttributedString 如何在NSMutableAttributedString中传递两个字符串 - How to pass two string in NSMutableAttributedString 在Swift 3中将属性字符串替换为NSMutableAttributedString - Replace attributed string into NSMutableAttributedString In Swift 3 NSMutableAttributedString上的临时属性在编辑字符串时被删除 - Temporary attributes on NSMutableAttributedString that are removed when string is edited 重置NSMutableAttributedString字符串是不是应用了它的属性? - Resetting NSMutableAttributedString string is not applying its attributes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM