Tag: nsattributedstring

使用NSTextAttachment设置NSAttributedString的截尾的垂直alignment

我使用下面的代码在iOS 8中为UILabel生成一个NSAttributedString 。 // a long long Chinese title NSString *title = @"这是一个很长很长很长很长很长很长的中文标题"; // setup icon attachment NSTextAttachment *iconAttachment = [[NSTextAttachment alloc] init]; iconAttachment.image = [UIImage imageNamed:imageName]; iconAttachment.bounds = bounds; NSAttributedString *ycardImageString = [NSAttributedString attributedStringWithAttachment:iconAttachment]; // setup attributed text NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:title]; if (shouldShowYcard) { [attributedText insertAttributedString:ycardImageString atIndex:0]; [attributedText insertAttributedString:[[NSAttributedString alloc] initWithString:@" […]

如何在NSTextAttachment中设置模板图像的颜色

如何设置作为属性string附件的模板化图像的颜色? 背景: 我有一个UILabel,我将其设置为一个NSAttributedString。 NSAttributedString包含一个带有小图片的NSTextAttachment。 现在我想让我的图像颜色匹配文本颜色,我无法弄清楚如何使其工作。 我通常希望通过将其渲染模式设置为UIImageRenderingModeAlwaysTemplate,然后在包含UIView上设置tintColor来着色图像。 我试过在我的UILabel上设置tintColor,但是没有效果。 这是我的代码。 它在Ruby(RubyMotion)中,所以语法可能看起来有点有趣,但是它与ObjectiveC 1:1映射。 attachment = NSTextAttachment.alloc.initWithData(nil, ofType: nil) attachment.image = UIImage.imageNamed(icon_name).imageWithRenderingMode(UIImageRenderingModeAlwaysTemplate) label_string = NSMutableAttributedString.attributedStringWithAttachment(attachment) label_string.appendAttributedString(NSMutableAttributedString.alloc.initWithString('my text', attributes: { NSFontAttributeName => UIFont.preferredFontForTextStyle(UIFontTextStyleFootnote), NSForegroundColorAttributeName => foreground_color })) label = UILabel.alloc.initWithFrame(CGRectZero) label.tintColor = foreground_color label.attributedText = label_string label.textAlignment = NSTextAlignmentCenter label.numberOfLines = 0

围绕UILabel的每一个字的边界

有没有一种方法可以在UILabel每个单词上绘制边界。 假设UILabel包含string“This is the Line 1”。 我想5个不同的边界5个字左右 这个 是 该 线 1

NSAttributedString:设置FontAttributes不会改变字体

我正在尝试更改NSAttributedString上的字体。 我有一个迭代每个字符的方法,并为该字符创build一个新的NSFontAttribute字典。 但是,最后,string保持不变。 为了演示,这里是我如何设置string: UIFontDescriptor *fontDescriptor = [UIFontDescriptor fontDescriptorWithName:@"Avenir-Book" size:14.0f]; NSDictionary *fontAttributes = [fontDescriptor fontAttributes]; [fontAttributes setValue:[NSString stringWithFormat:@"%u",[fontDescriptor symbolicTraits]] forKey:UIFontSymbolicTrait]; [mutableAttributedString setAttributes:fontAttributes range:(NSRange){0,length}]; 这将为整个string生成以下NSFontAttribute字典: Font Attributes: { NSCTFontSymbolicTrait = 2147483648; NSFontNameAttribute = "Avenir-Book"; NSFontSizeAttribute = 14; } 我通过并修改每个字符的FontAttribute来添加粗体或斜体,如下所示: for (int i = (int)range.location; i < (range.location + range.length); i++){ /* Extract Font Attributes */ NSDictionary […]

当使用图像和文本时,NSAttributeString高度似乎alignment错误

我在tableviewcell中有一个uilabel,标签使用NSAttributeString来支持图像和文本。 但是我发现,当标签仅包含图片或文字时,标签的高度是正确的,但是当标签中包含图片和文字时,高度似乎不正确,行高看起来比实际高度需要量稍大 如果只有文字或图片,高度是正确的 我使用NSTextAttachment的自己的子类来构build图像 和附件代码是这样的

我可以在NSAttributedString中获取元素的位置吗?

我使用NSAttributeString来获得像上面这样的东西(包括普通文本,带下划线的文本和图像),现在我想要做的事情是:如果用户点击加下划线的文本区域,我会做一些特殊的操作(打开网页或者某事)。 现在我可以得到触摸的位置,但我可以find加下划线的文本的位置(或区域)? 所以我可以使用这两个区域来判断分接位置是否位于下划线文字上?

TTTAttributedLabel“阅读更多>”可能有几个属性的尾部截断?

TTTAttributedLabel通过truncationTokenString以及truncationTokenStringAttributes支持一个自定义的截断string。 但是,我想要进一步并在截断string上设置几个string属性,包括不同的字体和颜色。 这是我想要实现的: 最后的箭头可以使用字体图标来实现,所以我想到了下面的string: @"… Read More >" 'HORIZONTAL ELLIPSIS' (U+2026) + > character from a font Read More + > character from a font 。 不幸的是, TTTAttributedLabel不允许我设置各种属性的范围。 有没有人有一个很好的解决scheme,或将不得不手动这样做,基本上计算string,因为它可以在两行,包括@"… Read More >"string。 谢谢!

iPhone上的CoreText和删除线

我目前正在努力在许多UITableViewCell显示删除线文本的需要。 用HTML编写的东西就像looke一样 <strike>€99 </ strike>保存50%=>现在€49 我不想仅仅为一行文本使用UIWebView ,特别是在小型UITableViewCell使用它。 我知道有可重复使用的单元格,但是我想保留更有效的内存方式。 所以…我使用的是NSAttributedString ,在AliSoftware的UILabel OHAttributedLabel OHAttributedLabel的帮助下。 事实上,只有从iOS 4.0开始才是可用的,因为我们使用各种与4.0兼容的东西。 我可以设法创build属性string,它显示OHAttributedLabel中的OHAttributedLabel ,好的,这很酷。 但是,我无法实现的是设置“删除线”或“删除线”属性。 基本上我是这样的: NSString * price = [NSString stringWithFormat:@“%01.2f€”,product.price]; NSString * rate = [NSString stringWithFormat:@“ – %01.0f %%”,product.reductionRate]; NSMutableAttributedString * s = [NSMutableAttributedString attributedStringWithString:price]; [s addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlinePatternSolid | NSUnderlineStyleSingle] range:NSRangeFromString(price)]; [归档标签setAttributedText:s]; 但是在这里,三个NS*常量是不确定的。 我已经导入CoreText.h , Foundation/NSAttributedString.h ,无济于事。 我在网上看到了NSStrikethroughStyleAttributeName = @"NSStrikethroughStyleAttributeName" […]

分割归属string并保留格式

你怎么能拿一个现有的NSAttributedString,并根据预定义的分隔符来划分它,同时保持格式? 似乎componentsSeparatedByString不会在NSAttributedString上运行。 我目前的解决方法产生正确的点拆分,但只输出一个NSString。 因此失去格式。 NSData *rtfFileData = [NSData dataWithContentsOfFile:path]; NSAttributedString *rtfFileAttributedString = [[NSAttributedString alloc] initWithData:rtfFileData options:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType} documentAttributes:nil error:nil]; NSString *rtfFileString = [rtfFileAttributedString string]; NSString *importSeparator = @"###"; // Wish I could do this // NSArray *separatedArray = [rtfFileAttributedString componentsSeparatedByString:importSeparatorPref]; NSArray *separatedArray = [rtfFileString componentsSeparatedByString:importSeparatorPref]; NSLog( @"Separated array: %@", separatedArray );

如何检测NSAttributedString是否包含NSTextAttachment并将其删除?

我收到一个NSAttributedString作为input,它可能包含一个作为NSTextAttachment附加的图像。 我需要检查是否附加了这样的图像,并在这种情况下,删除它。 我一直在寻找相关的职位没有成功,我怎么能这样做? 编辑:我想这个: let mutableAttrStr = NSMutableAttributedString(attributedString: textView.attributedText) textView.attributedText.enumerateAttribute(NSAttachmentAttributeName, in: NSMakeRange(0, textView.attributedText.length), options: NSAttributedString.EnumerationOptions(rawValue: 0)) { (value, range, stop) in if (value as? NSTextAttachment) != nil { mutableAttrStr.replaceCharacters(in: range, with: NSAttributedString(string: "")) } } 如果textView.attributedText包含多个附件(我在其string看到了几个\u{ef} ),我希望枚举匹配条件if (value as? NSTextAttachment) != nil几次,但是该代码块是只执行一次。 我如何删除所有附件?