黑体和非粗体文字在一个单一的UILabel?如何在uiLabel中同时包含粗体和非粗体文本?我宁愿不使用UIWebView.。我也读过使用NSAttributedString可能是可能的,但是我不知道如何使用它。有什么想法吗?苹果公司在他们的几个应用程序中实现了这一点;
3 回答
慕盖茨4494581
TA贡献1850条经验 获得超11个赞
下面是它的用法:
myLabel.text = @"Updated: 2012/10/14 21:59 PM";[myLabel boldSubstring: @"Updated:"];[myLabel boldSubstring: @"21:59 PM"];
UILabel+Boldify.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];}
慕尼黑5688855
TA贡献1848条经验 获得超2个赞
UILabel
UILabel+Boldify.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];}@endmyLabel.text = @"Updated: 2012/10/14 21:59 PM";[myLabel boldSubstring: @"Updated:"];[myLabel boldSubstring: @"21:59 PM"];
- 3 回答
- 0 关注
- 670 浏览
添加回答
举报
0/150
提交
取消
