黑體和非粗體文字在一個單一的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];}@end
myLabel.text = @"Updated: 2012/10/14 21:59 PM";[myLabel boldSubstring: @"Updated:"];[myLabel boldSubstring: @"21:59 PM"];
- 3 回答
- 0 關注
- 646 瀏覽
添加回答
舉報
0/150
提交
取消