ios – 使用NSTextAttachment为NSAttributedString设置截断尾部的垂直对齐

我正在使用以下代码为iOS 8中的UILabel生成NSAttributedString.
// a long long Chinese title 
Nsstring *title = @"这是一个很长很长很长很长很长很长的中文标题";
// setup icon attachment
NSTextAttachment *iconAttachment = [[NSTextAttachment alloc] init];
iconAttachment.image = [UIImage imageNamed:imageName];
iconAttachment.bounds = bounds;
NSAttributedString *ycardImageString = [NSAttributedString attributedStringWithAttachment:iconAttachment];

// setup attributed text
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:title];
if (shouldShowYcard) {
    [attributedText insertAttributedString:ycardImageString atIndex:0];
    [attributedText insertAttributedString:[[NSAttributedString alloc] initWithString:@" "] atIndex:1];
    [attributedText addAttribute:NSBaselineOffsetAttributeName value:@(offset) range:NSMakeRange(0,1)];
}
NSRange titleRange = NSMakeRange(shouldShowYcard ? 2 : 0,title.length);
[attributedText addAttribute:NSFontAttributeName value:font range:titleRange];
[attributedText addAttribute:NSForegroundColorAttributeName value:color range:titleRange];

然而,似乎NSTextAttachment将影响截断尾部的垂直位置,就像下面的图片一样.




有没有办法为截断的尾部设置垂直对齐?

我的目标是用中文底部对齐尾部.

这是test.的图标

解决方法

试试这一个
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(50,300,30)];
    [self.view addSubview:lbl];
    Nsstring *t= @"这是一个很长很长很长很长很长很长的中文标题漢字";
    NSTextAttachment *iconatt = [[NSTextAttachment alloc]init];
    iconatt.image = [UIImage imageNamed:@"phnzY.png"];
    iconatt.bounds = CGRectMake(0,44,22);

    NSAttributedString *ycardstring = [NSAttributedString attributedStringWithAttachment:iconatt];
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:t];
    [attributedText insertAttributedString:ycardstring atIndex:0];
    [attributedText insertAttributedString:[[NSAttributedString alloc] initWithString:@" "] atIndex:1];
     [attributedText addAttribute:NSBaselineOffsetAttributeName value:@(0.0) range:NSMakeRange(0,1)];
        NSRange titleRange = NSMakeRange( 0,t.length);
    [attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22.0] range:titleRange];
    [attributedText addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:titleRange];
    lbl.attributedText = attributedText;

它会像这样给出输出

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...