Ios NSTextAttachment在UILabel中的位置?
我有一个Ios NSTextAttachment在UILabel中的位置?,ios,objective-c,uilabel,nsattributedstring,nstextattachment,Ios,Objective C,Uilabel,Nsattributedstring,Nstextattachment,我有一个ui标签显示nsattributed字符串。字符串包含文本和UIImage作为NSTextAttachment 渲染时,是否有方法获取ui标签中NSTextAttachment的位置 编辑 这是我试图达到的最终结果 当文本只有一行长时,图像应该正好位于ui标签的边缘。简单: 如果有多行,但仍希望图像位于最后一行的末尾,则会出现问题: 我可以想出一种解决方案(更像是一种变通方法),它只适用于有限的情况。假设您的NSAttributedString左侧包含文本,右侧包含图像,则可以使用计算
ui标签
显示nsattributed字符串
。字符串包含文本和UIImage
作为NSTextAttachment
渲染时,是否有方法获取ui标签中NSTextAttachment
的位置
编辑
这是我试图达到的最终结果
当文本只有一行长时,图像应该正好位于ui标签的边缘。简单:
如果有多行,但仍希望图像位于最后一行的末尾,则会出现问题:
我可以想出一种解决方案(更像是一种变通方法),它只适用于有限的情况。假设您的NSAttributedString
左侧包含文本,右侧包含图像,则可以使用计算文本大小并获取NSTextAttachment
的位置。这不是一个完整的解决方案,因为只能使用x
坐标(即文本部分的宽度
)
NSString *string = @"My Text String";
UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Italic" size:24.0];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
CGSize size = [string sizeWithAttributes:attributes];
NSLog(@"%f", size.width); // this should be the x coordinate at which your NSTextAttachment starts
希望这能给你一些提示
编辑:
如果有换行功能,可以尝试以下代码(string
是要放入UILabel的字符串,self.testLabel
是UILabel):
谢谢这是我目前正在做的,但是如果你的UILabel正在换行,它将不起作用。如果你计算换行呢<代码>CGFloat总宽度=0;NSArray*wordArray=[string componentsSeparatedByString:@”“];对于(wordArray中的NSString*i){UIFont*font=[UIFontWithName:@“HelveticaNeue Italic”大小:10.0];NSDictionary*attributes=[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil];//获取字符串的大小,向其添加空格CGSize stringSize=[[i stringByAppendingString:@”“]sizeWithAttributes:attributes];totalWidth+=stringSize.width;//获取空格字符CGSize spaceSize=[@”“sizeWithAttributes:attributes];
//如果此“if”为真,那么我们将使用换行if((totalWidth-spaceSize.width)>self.testLabel.frame.size.width){//并且我们的宽度将仅为新行totalWidth=stringSize.width;}}}//上的字符串大小,这可以防止出现错误,如果(textAttachment.image.size.width>self.testLabel.frame.size.width-totalWidth){totalWidth=0;}NSLog(@“%f”,totalWidth)
很抱歉格式不正确。请使用该wall-o-code编辑您的文章。注释使用不当。@rdougan也许您可以创建一个图像,右侧包含箭头图像,左侧为空(透明)。图像的总宽度将是上面代码计算的宽度,图像将动态生成(如此处所示:)对于每个UILabel。不是最漂亮的东西,我也不确定箭头图像是否对齐,但您可能可以尝试一下。是否有特定的原因必须以这种特定的方式进行此操作?您正试图实现的模拟可能会产生更优的替代方法。无论哪种方式,您都不能只添加ima吗ge作为UILabel
?@BradAllred的子视图,我已经用我想要实现的目标更新了我的帖子。
CGFloat totalWidth = 0;
NSArray *wordArray = [string componentsSeparatedByString:@" "];
for (NSString *i in wordArray) {
UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Italic" size:10.0];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
// get the size of the string, appending space to it
CGSize stringSize = [[i stringByAppendingString:@" "] sizeWithAttributes:attributes];
totalWidth += stringSize.width;
// get the size of a space character
CGSize spaceSize = [@" " sizeWithAttributes:attributes];
// if this "if" is true, then we will have a line wrap
if ((totalWidth - spaceSize.width) > self.testLabel.frame.size.width) {
// and our width will be only the size of the strings which will be on the new line minus single space
totalWidth = stringSize.width - spaceSize.width;
}
}
// this prevents a bug where the end of the text reaches the end of the UILabel
if (textAttachment.image.size.width > self.testLabel.frame.size.width - totalWidth) {
totalWidth = 0;
}
NSLog(@"%f", totalWidth);