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

时间:2011-05-16 06:04:56

标签: objective-c ios nsstring

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

  

近似距离: 120米

谢谢!

11 个答案:

答案 0 :(得分:95)

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...

查看NSAttributedString以使用核心文字绘制NSAttributedString个对象。

答案 1 :(得分:60)

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

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];

答案 2 :(得分:11)

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

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

答案 3 :(得分: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];

答案 4 :(得分:4)

<强>夫特

还包括获取要动态增强的字符串范围

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

答案 5 :(得分:4)

要粗体化字符串而不对其字体进行硬编码,可以使用带有负值的StrokeWidth属性:

let s = NSMutableAttributedString(string: "Approximate Distance: 120m away")
s.addAttribute(NSStrokeWidthAttributeName, value: NSNumber(value: -3.0), range: NSRange(22..<26))

答案 6 :(得分:3)

NSString只是一个数据容器。它不包含有关演示问题的任何详细信息。

听起来你可能想要做的是用于显示你的字符串的UILabel的粗体部分。我不认为你能做到。但你总是可以将用户界面分成三个标签,一个用于“近似距离:”,一个用于“ 120米”,一个用于“离开”。将它们放在一起,你应该得到所需的效果。

另一种选择可能是使用UIWebView和一点标记来显示带有嵌入格式信息的字符串,如下所述:

http://iphoneincubator.com/blog/windows-views/display-rich-text-using-a-uiwebview

答案 7 :(得分:2)

在Xamarin ios中,您可以通过这种方式加强NSString的一部分:

public static NSMutableAttributedString BoldRangeOfString (string str, float fontSize, int startRange, int lengthRange)
    {
        var firstAttributes = new UIStringAttributes {
            Font = UIFont.BoldSystemFontOfSize(fontSize)
        };

        NSMutableAttributedString boldString = new NSMutableAttributedString (str);
        boldString.SetAttributes (firstAttributes.Dictionary, new NSRange (startRange, lengthRange));
        return boldString;
    }    

并调用此方法:

myLabel = new UILabel (); 
...
myLabel.AttributedText = BoldRangeOfString("my text", fontSize, startRange, lengthRange);    

答案 8 :(得分:2)

我联系了@Jacob Relkin和@Andrew Marin的答案,否则,我得到了崩溃。以下是iOS9的答案:

UIFont *boldFont = [UIFont boldSystemFontOfSize:12];
NSString *yourString = @"Approximate Distance: 120m away";
NSRange boldedRange = NSMakeRange(22, 4);

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

[attrString beginEditing];
[attrString addAttribute:NSFontAttributeName 
                   value:boldFont
                   range:boldedRange];

[attrString endEditing];

我查看了官方文档:12

答案 9 :(得分:1)

使用Swift5 +的更短的方式

let labelNotes = UILabel()  //or UITextView(), etc...
let attributedNotes = NSMutableAttributedString(string: "Bold: some stuff not bold")
attributedNotes.addAttribute(NSAttributedString.Key.font, value: UIFont.boldSystemFont(ofSize: 14), range: NSRange(location: 0, length: 5))
labelNotes.attributedText = attributedNotes

答案 10 :(得分:0)

如果您不想对字体或/和大小进行硬编码,请尝试以下代码以粗体显示完整字符串:

NSMutableAttributedString *myString = [[NSMutableAttributedString alloc] initWithString:mainString];
[myString beginEditing];
[myString addAttribute:NSStrokeWidthAttributeName
                         value:[[NSNumber alloc] initWithInt: -3.f]
                         range:NSMakeRange(0, [mainString length])];
[myString endEditing];