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

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

概述我正在使用以下代码为iOS 8中的UILabel生成NSAttributedString. // a long long Chinese title NSString *title = @"这是一个很长很长很长很长很长很长的中文标题";// setup icon attachmentNSTextAttachment *iconAttachment = [[NSTextAttachment a 我正在使用以下代码为iOS 8中的UILabel生成NSAttributedString.
// a long long Chinese Title Nsstring *Title = @"这是一个很长很长很长很长很长很长的中文标题";// setup icon attachmentNSTextAttachment *iconAttachment = [[NSTextAttachment alloc] init];iconAttachment.image = [UIImage imagenamed:imagename];iconAttachment.bounds = bounds;NSAttributedString *ycardImageString = [NSAttributedString attributedStringWithAttachment:iconAttachment];// setup attributed textNSMutableAttributedString *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 – 使用NSTextAttachment为NSAttributedString设置截断尾部的垂直对齐全部内容,希望文章能够帮你解决ios – 使用NSTextAttachment为NSAttributedString设置截断尾部的垂直对齐所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1103809.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存