任何方式来加强NSString的一部分?

Zhe*_*hen 72 objective-c nsstring ios

有没有办法只加粗字符串的一部分?例如:

近似距离:120米远

谢谢!

Jac*_*kin 97

你可以做的是使用的NSAttributedString.

NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName];
NSString *yourString = ...;
NSRange boldedRange = NSMakeRange(22, 4);

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString];

[attrString beginEditing];
[attrString addAttribute:kCTFontAttributeName 
                   value:boldFontName
                   range:boldedRange];

[attrString endEditing];
//draw attrString here...
Run Code Online (Sandbox Code Playgroud)

看看这个方便的花花公子指南,NSAttributedString用Core Text 绘制对象.

  • 这个在iOS7上不起作用(与exc_bad_access崩溃).原因是你必须发送字体而不是字体名称作为消息的一部分.@Chris Frederick在下面正确地做了. (11认同)
  • 它说:使用未定义的标识符"kCTFontAttributeName"的,你的意思是"NSFontAttributeName"?这有什么不对?我可以使用NSFontAttributeName吗? (8认同)
  • 谁能告诉我"kCTFontAttributeName"被定义为什么? (2认同)

Chr*_*ick 60

正如雅各布所说,你可能想要使用一个NSAttributedString或一个NSMutableAttributedString.以下是如何执行此操作的一个示例.

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Approximate Distance: 120m away"];
NSRange selectedRange = NSMakeRange(22, 4); // 4 characters, starting at index 22

[string beginEditing];

[string addAttribute:NSFontAttributeName
           value:[NSFont fontWithName:@"Helvetica-Bold" size:12.0]
           range:selectedRange];

[string endEditing];
Run Code Online (Sandbox Code Playgroud)

  • @ZuluDeltaNiner如果您使用的是iOS,请将`NSFont`替换为`UIFont` (12认同)
  • 救了我!这个问题的答案需要编辑我花了两个小时就这个&^%# (2认同)

yur*_*ura 12

如果您不想打扰字体(因为并非每个字体的变体都包含"Bold"),这是另一种方法.请注意,目前仅适用于OS X ...:

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:"Approximate Distance: 120m away"];
[attrString beginEditing];
[attrString applyFontTraits:NSBoldFontMask
                      range:NSMakeRange(22, 4)];
[attrString endEditing];
Run Code Online (Sandbox Code Playgroud)


And*_*rin 6

上面的代码让我在使用这个referencedString创建UILabel时崩溃了.

我使用了这段代码并且它有效:

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSRange boldedRange = NSMakeRange(0, 1);
UIFont *fontText = [UIFont systemFontOfSize:12]; //[UIFont fontWithName:@"Lato-Bold" size:12];
NSDictionary *dictBoldText = [NSDictionary dictionaryWithObjectsAndKeys:fontText, NSFontAttributeName, nil];
[attrString setAttributes:dictBoldText range:boldedRange];
Run Code Online (Sandbox Code Playgroud)


Mag*_*goo 5

迅速

还包括获取要动态加粗的字符串的范围

let nameString = "Magoo"
let string = "Hello my name is \(nameString)"

let attributes = [NSFontAttributeName:UIFont.systemFontOfSize(14.0),NSForegroundColorAttributeName: UIColor.black]
let boldAttribute = [NSFontAttributeName:UIFont.boldSystemFontOfSize(14.0)]

let attributedString = NSMutableAttributedString(string: string, attributes: attributes)

let nsString = NSString(string: string)
let range = nsString.rangeOfString(nameString)

if range.length > 0 { attributedString.setAttributes(boldAttribute, range: range) }

someLabel.attributedText = attributedString
Run Code Online (Sandbox Code Playgroud)