ios - 使用 NSTextAttachment 为 NSAttributedString 设置截尾的垂直对齐

标签 ios alignment uilabel nsattributedstring nstextattachment

我正在使用以下代码为 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 似乎会影响截尾的垂直位置,如下图所示。

NSAttributedString with NSTextAttachment NSAttributedString without NSTextAttachment NSAttributedString with NSTextAttachment In English NSAttributedString without NSTextAttachment In English

有没有办法设置截尾的垂直对齐方式?

我的目标是让中文尾部底部对齐。

这是一个测试图标。 icon

最佳答案

试试这个

   UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(50, 300, 300, 30)];
    [self.view addSubview:lbl];
    NSString *t= @"这是一个很长很长很长很长很长很长的中文标题漢字";
    NSTextAttachment *iconatt = [[NSTextAttachment alloc]init];
    iconatt.image = [UIImage imageNamed:@"phnzY.png"];
    iconatt.bounds = CGRectMake(0, 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;

它会给出这样的输出

output

关于ios - 使用 NSTextAttachment 为 NSAttributedString 设置截尾的垂直对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39643542/

相关文章:

objective-c - 更好地控制 SenTesting Kit/OCUnit 中的测试套件

CSS:如何使自动建议结果与输入字段对齐

swift - UILabel 文本未在 Swift 中单击的 UIButton 上更新

ios - UILabel 在垂直增长和断线时添加不必要的顶部和底部填充

html - CSS - 如何让不同 parent 的 child 高度相同?

ios - UILabel的垂直对齐算法是什么?

ios - UIPopoverPresentationController 错位

iphone - 将 uitableview 重新加载到底部单元格

iphone - 使用 UIPopover 从图库中获取图像并显示它

html css对齐