【问题标题】:Different font size in the same label?同一标签中的不同字体大小?
【发布时间】:2013-01-17 11:16:38
【问题描述】:

在同一个 UILabel 中是否可以有不同的字体大小或粗细?我可以在情节提要中将其作为属性标签进行,但我需要以编程方式进行。

cell.lblOne.text = [NSString stringWithFormat:
                       @"FontSize15:: %@, FontSize20:: %@",monkey, goat];

编辑:我看到了一些关于 NSAttributedString 的东西,但我无法让它工作。

【问题讨论】:

    标签: ios uilabel nsattributedstring uifont


    【解决方案1】:

    在这里看看我的回答:

    UITextView Alternative

    • 制作一个 NSMutableAttributedString
    • 给它一些属性(应用于字符范围)
    • 设置标签的属性文本属性

    .

     NSMutableAttributedString *attString = 
                                  [[NSMutableAttributedString alloc]
                                            initWithString: @"monkey goat"];
    
    [attString addAttribute: NSForegroundColorAttributeName
                      value: [UIColor redColor]
                      range: NSMakeRange(0,6)];
    
    
    [attString addAttribute: NSFontAttributeName
                      value:  [UIFont fontWithName:@"Helvetica" size:15]
                      range: NSMakeRange(0,6)];
    
    [attString addAttribute: NSFontAttributeName
                      value:  [UIFont fontWithName:@"Didot" size:24]
                      range: NSMakeRange(7,4)];
    
    self.label.attributedText  = attString;
    

    【讨论】:

    • 有没有办法限制字符串中第一个 %@ 的范围?或者我需要计算字符串中的单词以获得正确的范围?有道理吗? :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-31
    • 2020-03-27
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多