【问题标题】:How to use attributed string as navigation bar title in iOS 5.0?如何在 iOS 5.0 中使用属性字符串作为导航栏标题?
【发布时间】:2013-10-17 06:20:20
【问题描述】:

有没有办法在 iOS 5 的导航栏上使用属性标签?我知道它适用于 iOS 6+,但在那之前?
我想在那里同时使用粗体和非粗体。

【问题讨论】:

    标签: ios ios5


    【解决方案1】:

    您可以使用TTAttributedLabel 来实现此目的。这是解决问题的代码。假设您要将标题设置为“Sample NavBarTitle”,其中 Sample 应为粗体。

    //In your code there may be several ranges, corresponding to how many attributes you want to have
    NSRange range = NSMakeRange(0, 6);
    //Initialize NSMutableAttributedString
    NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"Sample NavBarTitle"];
    //Set the attribute to make "Sample" bold
    [string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Georgia-Bold" size:15] range:range];
    //Initialize TTAttributedLabel with rect
    TTTAttributedLabel * label = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0, 0, 20, 150)];
    //Set the attributedText property of TTAttributedLabel
    label.attributedText = string;
    //Set navigationItem.titleView to the label view we've created
    self.navigationItem.titleView = label;
    

    就是这样。

    【讨论】:

    • 这看起来不错,但 NSFontAttributeName 是 ios6 唯一的枚举。我可以用什么代替?
    • @Dvole 这个答案很好地回答了你的问题,所以请接受它。如果您对 NSFontAttributeName 感兴趣,喜欢其他 iOS 版本,添加新问题,或编辑您当前的问题以包含它。
    • @FahriAzimov 不是真的,它在 iOS 5 中崩溃了。
    • 尝试使用kCTFontAttributeName,如果也不起作用,我会尝试提供另一种解决方案。
    【解决方案2】:

    这是帮助我的代码:

     // Total string
        NSString *tempString = [NSString stringWithFormat:@"%@, %@ %@ %@ %@", gameName, stringConnector, self.bet.gameInfo.draw.drawNumber, stringConnector2,  [dateFormatter stringFromDate:self.bet.gameInfo.draw.date]];
    
        NSRange range = [tempString rangeOfString:gameName];
    
        NSMutableAttributedString * string2 = [[NSMutableAttributedString alloc] initWithString:tempString];
    
        // font for all string
        UIFont *regularFont = [UIFont fontWithName:BEAU_PRO_LIGHT_FONT_NAME size:21];
        CTFontRef font_2 = CTFontCreateWithName((__bridge CFStringRef)regularFont.fontName, regularFont.pointSize, NULL);
        [string2 addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font_2 range:[tempString rangeOfString:tempString]];
    
        // bold font
        UIFont *boldFont = [UIFont fontWithName:BEAU_PRO_SEMIBOLD_FONT_NAME size:21];
        CTFontRef font_1 = CTFontCreateWithName((__bridge CFStringRef)boldFont.fontName, boldFont.pointSize, NULL);
        [string2 addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font_1 range:range];
    
        TTTAttributedLabel * label = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0, 0, 600, 20)];
        label.attributedText = string2;
        [label sizeToFit];
        self.navigationItem.titleView = label;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 2014-12-28
      • 1970-01-01
      • 1970-01-01
      • 2017-11-08
      相关资源
      最近更新 更多