NSAttributedString superscript styling

后端 未结 4 526
南笙
南笙 2020-11-29 04:27

I want to superscript all the instances of ® character in a block of text (legal disclaimer, naturally ;)) and the default way NSAttributedString is not very go

相关标签:
4条回答
  • 2020-11-29 05:00

    Swift 5

        let fnt = UIFont(name:"Helvetica", size:20.0)
        let attributedString = NSMutableAttributedString(string:"2.099", attributes:[NSAttributedString.Key.font : fnt!])
        attributedString.setAttributes([NSAttributedString.Key.font : fnt!.withSize(10), NSAttributedString.Key.baselineOffset: 10], range: NSRange(location: 4, length: 1))
    
    0 讨论(0)
  • 2020-11-29 05:02

    Swift 4.2

    In my example I want to subscript one instance of infinity symbol so my label's title will look like this:

    let font = UIFont(name: "Helvetica", size: 14.0)
    
    let attributedString = NSMutableAttributedString(string: "Solids(ΔE∞)•G7®", attributes: [NSAttributedStringKey.font : font!])
    
    attributedString.setAttributes([NSAttributedStringKey.baselineOffset: -5], range: NSRange(location: 9, length: 1))
    
    solidsLbl.attributedText = attributedString
    
    0 讨论(0)
  • 2020-11-29 05:14

    The following code seems to do the trick:

    UIFont *fnt = [UIFont fontWithName:@"Helvetica" size:20.0];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"GGG®GGG"
                                                                                         attributes:@{NSFontAttributeName: [fnt fontWithSize:20]}];
    [attributedString setAttributes:@{NSFontAttributeName : [fnt fontWithSize:10]
                                      , NSBaselineOffsetAttributeName : @10} range:NSMakeRange(3, 1)];
    

    enter image description here

    0 讨论(0)
  • 2020-11-29 05:15

    Swift version:

    let fnt = UIFont(name:"Helvetica", size:20.0)
    let attributedString = NSMutableAttributedString(string:"GGG®GGG", attributes:[NSFontAttributeName : fnt!])
    attributedString.setAttributes([NSFontAttributeName : fnt!.fontWithSize(10), NSBaselineOffsetAttributeName: 10], range: NSRange(location: 3, length: 1))
    
    0 讨论(0)
提交回复
热议问题