有什么办法可以只对字符串的一部分加粗吗?例如:
大约距离:120米远
谢谢!
Answers:
您可以做的是使用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...
看看这个方便的花哨的指南,NSAttributedString
用Core Text绘制对象。
NSFontAttributeName
只能在OS X
正如Jacob所提到的,您可能想要使用anNSAttributedString
或an 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];
Use of Undeclared identifier 'NSFont'
上value:
线了。我该如何解决?
@
之后,示例字符串附近缺少initWithString:
。:)
NSFont
为UIFont
如果您不想打扰字体(因为并非每个字体的变体都包含“粗体”),这是另一种方法。请注意,这仅在OS X上可用...:
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:"Approximate Distance: 120m away"];
[attrString beginEditing];
[attrString applyFontTraits:NSBoldFontMask
range:NSMakeRange(22, 4)];
[attrString endEditing];
当我使用此attributedString创建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];
迅速
还包括获取要动态加粗的字符串范围
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
AnNSString
只是一个数据容器。它不包含有关演示文稿问题的任何详细信息。
听起来您可能想要做的是UILabel
用于显示字符串的粗体部分。我认为您无法做到。但是您始终可以将UI分为三个标签,一个用于“近似距离:”,一个用于“ 120 m ”,一个用于“离开”。将它们彼此排成一行,您将获得理想的效果。
另一个选择可能是使用一UIWebView
点点标记来显示带有嵌入式格式信息的字符串,如此处所述:
http://iphoneincubator.com/blog/windows-views/display-rich-text-using-a-uiwebview
在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);
我加上@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];
使用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
如果您不想对字体或/和大小进行硬编码,请尝试以下代码以粗体显示完整的字符串:
NSMutableAttributedString *myString = [[NSMutableAttributedString alloc] initWithString:mainString];
[myString beginEditing];
[myString addAttribute:NSStrokeWidthAttributeName
value:[[NSNumber alloc] initWithInt: -3.f]
range:NSMakeRange(0, [mainString length])];
[myString endEditing];