【发布时间】:2011-08-26 04:51:43
【问题描述】:
有没有办法只加粗字符串的一部分? 例如:
大约距离:120m
谢谢!
【问题讨论】:
-
这取决于您显示它的内容,因为字符串只是一系列字符,不包含格式信息。
标签: objective-c ios nsstring
有没有办法只加粗字符串的一部分? 例如:
大约距离:120m
谢谢!
【问题讨论】:
标签: objective-c ios nsstring
NSString 只是一个数据容器。它不包含有关演示问题的任何详细信息。
听起来您可能想要做的是用于显示字符串的UILabel 的粗体部分。我认为你做不到。但是您总是可以将 UI 分解为三个标签,一个用于“Approximate Distance:”,一个用于“120 m”,一个用于“距离”。将它们彼此对齐放置,您应该会获得所需的效果。
另一种选择可能是使用UIWebView 和一些标记来显示带有嵌入格式信息的字符串,如下所述:
http://iphoneincubator.com/blog/windows-views/display-rich-text-using-a-uiwebview
【讨论】:
您可以做的是使用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...
看看this handy dandy guide 用Core Text 绘制NSAttributedString 对象。
【讨论】:
NSFontAttributeName 仅适用于 OS X。
正如 Jacob 提到的,您可能想要使用 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];
【讨论】:
value: 行上得到Use of Undeclared identifier 'NSFont'。我该如何解决这个问题?
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];
【讨论】:
[string mutableCopy] 来获取 NSAttributedString *string 实例的可变副本。
在 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);
【讨论】:
当我使用这个属性字符串创建 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];
【讨论】:
我将@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];
【讨论】:
斯威夫特
还包括动态获取要加粗的字符串范围
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
【讨论】:
要加粗字符串而不对其字体进行硬编码,可以使用带负值的 StrokeWidth 属性:
let s = NSMutableAttributedString(string: "Approximate Distance: 120m away")
s.addAttribute(NSStrokeWidthAttributeName, value: NSNumber(value: -3.0), range: NSRange(22..<26))
【讨论】:
如果您不想对字体或/和大小进行硬编码,请尝试使用以下代码来加粗整个字符串:
NSMutableAttributedString *myString = [[NSMutableAttributedString alloc] initWithString:mainString];
[myString beginEditing];
[myString addAttribute:NSStrokeWidthAttributeName
value:[[NSNumber alloc] initWithInt: -3.f]
range:NSMakeRange(0, [mainString length])];
[myString 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
【讨论】: