亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

黑體和非粗體文字在一個單一的UILabel?

黑體和非粗體文字在一個單一的UILabel?

慕妹3242003 2019-07-03 13:43:28
黑體和非粗體文字在一個單一的UILabel?如何在uiLabel中同時包含粗體和非粗體文本?我寧愿不使用UIWebView.。我也讀過使用NSAttributedString可能是可能的,但是我不知道如何使用它。有什么想法嗎?蘋果公司在他們的幾個應用程序中實現了這一點;
查看完整描述

3 回答

?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

在UILabel上嘗試一個類別:

下面是它的用法:

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];}

請注意,這將只在iOS 6和更高版本中工作。在iOS 5和更早版本中,它將被忽略。


查看完整回答
反對 回復 2019-07-03
?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

有基于bbrame分類的分類。它的工作原理類似,但允許您使用相同的方法。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"];


查看完整回答
反對 回復 2019-07-03
  • 3 回答
  • 0 關注
  • 646 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號