使用NSTextAttachment设置NSAttributedString的截尾的垂直alignment

我使用下面的代码在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似乎会影响截尾的垂直位置,就像下面的图片一样。

带有NSTextAttachment的NSAttributedString NSAttributedString没有NSTextAttachment 带有NSTextAttachment的NSAttributedString用英文 NSAttributedString没有NSTextAttachment英文

有没有办法设置截尾的垂直方向?

我的目标是用中文alignment底部。

这是一个testing的图标。 图标

试试这个

  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; 

它会给这个输出

产量

你可以尝试使用NSMutableParagraphStyle来设置paragraphStyle。 代码如下:

  let label1 = UILabel(frame: CGRect(x: 50, y: 50, width: 100, height: 30)) let label2 = UILabel(frame: CGRect(x: 50, y: 90, width: 100, height: 30)) view.addSubview(label1) view.addSubview(label2) let t = "12341421dsadyusatdiuwtquyidtywatyudigsaydgsadysaghdkgaugduiyyudgasgdj" let iconAttachment = NSTextAttachment() iconAttachment.image = UIImage(named: "caseEditorBtn0") iconAttachment.bounds = CGRect(x: 0, y: 0, width: 20, height: 30); let ycardImageString = NSAttributedString(attachment: iconAttachment) let yT0 = NSMutableAttributedString(string: t) let yT1 = NSMutableAttributedString(string: t) let p = NSMutableParagraphStyle() p.lineBreakMode = .byTruncatingTail //set line break model p.alignment = .right //set aligment yT1.setAttributes([NSParagraphStyleAttributeName: p], range: NSRange(location: 0, length: yT1.length)) yT0.setAttributes([NSParagraphStyleAttributeName: p], range: NSRange(location: 0, length: yT0.length)) //insert you custom text attachment at last, or it may be not show yT0.insert(ycardImageString, at: 0) label1.attributedText = yT0 label2.attributedText = yT1 

好的,最后我select了一个丑陋的方法来解决这个问题,通过设置truncate tail的NSBaselineOffsetAttributeName

这是计算截断部分的一种方法。 但在我的情况下,我的标签宽度是固定的,所以我可以直接设置控制的范围。

 if (title.length > 10 && ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0)) { [attributedText addAttribute:NSBaselineOffsetAttributeName value:@(-4) range:NSMakeRange(9, 1)]; }