NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init]; pStyle.tabStops = @[ [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentleft location:tabLocation options:[NSDictionary dictionary]] ]; NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init]; NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init]; textAttachment.image = [UIImage imagenamed:@"test_image"]; textAttachment.bounds = CGRectMake(0,-3,15,15);//resize the image attString = [NSAttributedString attributedStringWithAttachment:textAttachment].mutablecopy; [attString appendAttributedString:[[NSAttributedString alloc] initWithString:[Nsstring stringWithFormat:@"Title\t\u2022 %@",[@[ @"description1",@"description2" ] componentsJoinedByString:@"\n\t\u2022 "]] attributes:@{NSParagraphStyleAttributename : pStyle}]]; label.attributedText = attString;
我希望右边的列表保持对齐,但事实并非如此,这是我得到的结果:
我期望列表是这样对齐的:
>根据描述,位置参数有助于从左边距定位文本.所以这就是我们需要的,只需替换下面的行
pStyle.tabStops = @[ [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentleft location:tabLocation options:[NSDictionary dictionary]] ];
同
pStyle.tabStops = @[[[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentleft location:[self getTextLocationFor:@"test"] options:[NSDictionary dictionary]] ];
>添加getTextLocationFor:方法来计算位置,如下所示
-(CGfloat)getTextLocationFor:(Nsstring *)inputStr{ CGSize maximuminputStringWIDth = CGSizeMake(FLT_MAX,30); CGRect textRect = [inputStr boundingRectWithSize:maximuminputStringWIDth options:NsstringDrawingUseslineFragmentOrigin attributes:@{NSFontAttributename:[UIFont systemFontOfSize:15]} context:nil]; UIImageVIEw * testimage = [[UIImageVIEw alloc]initWithImage:[UIImage imagenamed:@"close_red"]];//Change image name with yours return textRect.size.wIDth + testimage.frame.size.wIDth +2;}
>那就是我们准备好运行你的项目,现在一切都会好的.
结果:
总结以上是内存溢出为你收集整理的ios – NSAttributedString带图像附件和NSTextTab,文本未对齐全部内容,希望文章能够帮你解决ios – NSAttributedString带图像附件和NSTextTab,文本未对齐所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)