如何在uiLabel中同时包含粗体和非粗体文本?
我宁愿不用UIWebView..我也读过这个可能使用NSAttributedString但我不知道如何使用它。什么好主意吗?
苹果在他们的几个应用程序中实现了这一点; 例子截图:
谢谢! ——Dom
如何在uiLabel中同时包含粗体和非粗体文本?
我宁愿不用UIWebView..我也读过这个可能使用NSAttributedString但我不知道如何使用它。什么好主意吗?
苹果在他们的几个应用程序中实现了这一点; 例子截图:
谢谢! ——Dom
当前回答
如果您希望更容易地使用带属性字符串,请尝试使用带属性字符串创建者,它将为您生成代码。https://itunes.apple.com/us/app/attributed-string-creator/id730928349
其他回答
试试UILabel上的分类:
下面是它的用法:
myLabel.text = @"Updated: 2012/10/14 21:59 PM";
[myLabel boldSubstring: @"Updated:"];
[myLabel boldSubstring: @"21:59 PM"];
这是分类
UILabel h +及。
- (void) boldSubstring: (NSString*) substring;
- (void) boldRange: (NSRange) range;
UILabel + Boldify.m
- (void) boldRange: (NSRange) range {
if (![self respondsToSelector:@selector(setAttributedText:)]) {
return;
}
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
[attributedText setAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:self.font.pointSize]} range:range];
self.attributedText = attributedText;
}
- (void) boldSubstring: (NSString*) substring {
NSRange range = [self.text rangeOfString:substring];
[self boldRange:range];
}
注意,这只适用于iOS 6及更高版本。在iOS 5或更早的版本中,它将被忽略。
我刚刚在我的项目中实现的以下代码不需要NSRange(在Swift中):
//Code sets label (yourLabel)'s text to "Tap and hold(BOLD) button to start recording."
let boldAttribute = [
//You can add as many attributes as you want here.
NSFontAttributeName: UIFont(name: "HelveticaNeue-Bold", size: 18.0)!
]
let regularAttribute = [NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 18.0)!]
let beginningAttributedString = NSAttributedString(string: "Tap and ", attributes: regularAttribute )
let boldAttributedString = NSAttributedString(string: "hold ", attributes: boldAttribute)
let endAttributedString = NSAttributedString(string: "button to start recording.", attributes: regularAttribute )
let fullString = NSMutableAttributedString()
fullString.appendAttributedString(beginningAttributedString)
fullString.appendAttributedString(boldAttributedString)
fullString.appendAttributedString(endAttributedString)
yourLabel.attributedText = fullString
NSString *needToChangeStr=@"BOOK";
NSString *display_string=[NSString stringWithFormat:@"This is %@",book];
NSMutableAttributedString *attri_str=[[NSMutableAttributedString alloc]initWithString:display_string];
int begin=[display_string length]-[needToChangeStr length];
int end=[needToChangeStr length];
[attri_str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30] range:NSMakeRange(begin, end)];
检查TTTAttributedLabel。它是UILabel的一个替换,它允许你在一个标签中混合字体和颜色,通过设置NSAttributedString作为标签的文本。
这是基于bbrame的category的category。它的工作原理类似,但允许您在累积结果中多次加粗相同的UILabel。
UILabel h +及。
@interface UILabel (Boldify)
- (void) boldSubstring: (NSString*) substring;
- (void) boldRange: (NSRange) range;
@end
UILabel + Boldify.m
@implementation UILabel (Boldify)
- (void)boldRange:(NSRange)range {
if (![self respondsToSelector:@selector(setAttributedText:)]) {
return;
}
NSMutableAttributedString *attributedText;
if (!self.attributedText) {
attributedText = [[NSMutableAttributedString alloc] initWithString:self.text];
} else {
attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
}
[attributedText setAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:self.font.pointSize]} range:range];
self.attributedText = attributedText;
}
- (void)boldSubstring:(NSString*)substring {
NSRange range = [self.text rangeOfString:substring];
[self boldRange:range];
}
@end
有了这个修正,你可以多次使用它,例如:
myLabel.text = @"Updated: 2012/10/14 21:59 PM";
[myLabel boldSubstring: @"Updated:"];
[myLabel boldSubstring: @"21:59 PM"];
将会得到:“更新日期:2012/10/14 21:59 PM”。