Ios 是否更改NSTextAttachment图像的大小?
我使用以下代码将RSS提要的原始HTML格式化为Ios 是否更改NSTextAttachment图像的大小?,ios,objective-c,nstextattachment,Ios,Objective C,Nstextattachment,我使用以下代码将RSS提要的原始HTML格式化为NSAttributedString。HTML包含标记,这些标记导致图像作为NSTextAttachment附加到NSAttributedString中。问题在于,对于放置属性字符串的UITextView,图像太大。有没有一种方法可以从属性字符串中操纵图像的大小?下面是代表图像附件的整个属性字符串的摘录 { NSAttachment = "<NSTextAttachment: 0x16d29450> \"e0e1d483a922
NSAttributedString
。HTML包含
标记,这些标记导致图像作为NSTextAttachment
附加到NSAttributedString
中。问题在于,对于放置属性字符串的UITextView,图像太大。有没有一种方法可以从属性字符串中操纵图像的大小?下面是代表图像附件的整个属性字符串的摘录
{
NSAttachment = "<NSTextAttachment: 0x16d29450> \"e0e1d483a922419807a2378a7ec031af.jpg\"";
NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
NSFont = "<UICTFont: 0x16ef8a40> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
NSKern = 0;
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 12, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
NSStrokeColor = "UIDeviceRGBColorSpace 0 0 0 1";
NSStrokeWidth = 0;
}
{
NSAttachment=“\”e0e1d483a922419807a2378a7ec031af.jpg\”;
NSColor=“UIDeviceRGBColorSpace 0 0 1”;
NSFont=“font-family:\“Times New Roman\”字体重量:正常;字体样式:正常;字体大小:12.00pt”;
NSKern=0;
NSParagraphStyle=“对齐4,行距0,段落间距12,段落间距0之前,头缩进0,尾缩进0,第一行头缩进0,线宽0/0,线宽倍数0,换行模式0,制表符(\n),默认TabInterval 36,块(null),列表(null),基线写入方向0,连字号因子0,拧紧因子0,头级别0”;
NSStrokeColor=“uiDeviceRbColorSpace 0 1”;
NSStrokeWidth=0;
}
您可以迭代NSAttribute字符串,获取附件、边界并修改它们:
[myAtrrString enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, myAtrrString.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (![value isKindOfClass:[NSTextAttachment class]]) {
return;
}
NSTextAttachment *attachment = (NSTextAttachment*)value;
CGRect bounds = attachment.bounds;
// modify bounds
attachment.bounds = bounds;
}];
这是我的解决方案。也许你会帮助别人
[yourSting enumerateAttribute:NSAttachmentAttributeName
inRange:NSMakeRange(0,[yourSting长度])
选项:0
使用块:^(id值、NSRange范围、布尔*停止)
{
if([value iskindof类:[NSTextAttachment类]])
{
NSTextAttachment*附件=(NSTextAttachment*)值;
UIImage*image=nil;
如果([附件图像])
图像=[附件图像];
其他的
image=[附件imageForBounds:[附件边界]
textContainer:nil
characterIndex:范围。位置];
CGSize size=image.size;
如果(size.width>kSCREEN_width-10){
//计算高度与宽度的比例
浮动比率=图像大小宽度/(k屏幕宽度-10);
浮动高度=image.size.height/比率;
尺寸=CGSizeMake(屏幕宽度-10,高度);
attachment.bounds=CGRectMake(0,0,size.width,size.height);
附件.图像=图像;
}
}
}]
您可以设置NSTextAttachment的边界,例如(在Swift3中):
let attachment = NSTextAttachment()
attachment.image = UIImage(named: "info.png")
attachment.bounds = CGRect(x: 5, y: -2, width: 15, height: 15)
let attachmentString = NSAttributedString(attachment: attachment)
let attributedString = NSMutableAttributedString(string: "My String")
attributedString.append(attachmentString)
labelText.attributedText = attributedString