问题描述
我正在使用以下代码在iOS 8中为 UILabel
生成 NSAttributedString
。
I'm using the following code to generate a NSAttributedString
for UILabel
in iOS 8.
// 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
似乎会生效截尾的垂直位置,如下图所示。
However it seems that the NSTextAttachment
will effect the vertical position of truncated tails, just like the following pictures.
有没有办法设置truncat的垂直高度
Is there a way to set the vertical aignment for the truncated tails?
我的目标是使中文尾巴对齐。
My goal is to have bottom align tails in Chinese language.
这是一个图标测试。
推荐答案
好,最后我选择了一种丑陋的方法来解决此问题 NSBaselineOffsetAttributeName
截尾。
OK, finally I choose a ugly way to fix this problem by setting NSBaselineOffsetAttributeName
for truncate tail.
是一种计算截断部分的方法。但是对于我来说,我的标签宽度是固定的,因此我可以直接设置控制范围。
Here is a way to calculate the truncate part. But in my case, my label width is fixed so that I can directly set the range for control.
if (title.length > 10 && ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0)) {
[attributedText addAttribute:NSBaselineOffsetAttributeName value:@(-4) range:NSMakeRange(9, 1)];
}
这篇关于使用NSTextAttachment为NSAttributedString设置截尾的垂直对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!