// 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设置截断尾部的垂直对齐所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)