Tag: nsattributedstring

以特定的方式将string转换为NSAttributedString

所以我有一个string,看起来像这样: Swift , VisualBasic , Ruby 我想转换这个string是这样的: 基本上我想创build一个单词后面的背景,是的,我可以使用NSTokenField库获取此行为,但我的文本不是用户手动input其预结构化(从一个数组),我不希望NSTokeField的整个行为,我只是想要这样的外观和select(通过select我的意思是清除一个单词在退格键,整个单词不是一个字母) 以及我知道如何改变一个文本的颜色是这样的 func getColoredText(text: String) -> NSMutableAttributedString { let string:NSMutableAttributedString = NSMutableAttributedString(string: text) let words:[String] = text.componentsSeparatedByString(" ") var w = "" for word in words { if (word.hasPrefix("{|") && word.hasSuffix("|}")) { let range:NSRange = (string.string as NSString).rangeOfString(word) string.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: range) w = word.stringByReplacingOccurrencesOfString("{|", withString: "") […]

设置属性文字,NSRangeException错误

尝试通过select设置我的UIText视图的归因文本属性。 几乎工作。 采取以下红色字体颜色设置文字的动作。 这有时会起作用,但经常会出现错误: 终止应用程序,由于未捕获的exception“NSRangeException”,原因:“NSMutableRLEArray objectAtIndex:effectiveRange ::越界” 即使在文本视图中看起来比选定范围所显示的字符多得多,也会发生这种情况。 – (IBAction)setText:(id)sender { NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithAttributedString:myTextView.attributedText]; [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(p1,p2)]; myTextView.attributedText = string; } p1和p2是所选文本的开始和结束。 它们是用下面的代码生成的,它看起来像预期的那样工作: – (void)textViewDidChangeSelection:(UITextView *)textView { UITextRange *selectedRange = [myTextView selectedTextRange]; p1 = [myTextView offsetFromPosition:myTextView.beginningOfDocument toPosition:[selectedRange start]]; p2 = [myTextView offsetFromPosition:myTextView.beginningOfDocument toPosition:[selectedRange end]]; } 编辑:我读了@ borrrden的评论后,解决了这个问题。 而不是NSMakeRange(p1,p2)]我正在使用NSMakeRange(p1,p2-p1)] 。

生成PDF时不显示NSAttributedString

这曾经工作,现在,突然间,它停止工作: 对于iOS 7 iPad应用程序,我生成一个PDF UIGraphicsBeginPDFContextToFile(pdfPathWithFileName, CGRectZero, nil); … UIGraphicsEndPDFContext(); 在那个代码块中,下面的方法用于渲染文本下划线,但是最近,它只是停止工作,传递给方法的任何文本都不会被渲染: +(void)drawUnderlinedText:(NSString *)text withFont:(UIFont *)font andColor:(UIColor *)color andLocationX:(int)locationX andLocationY:(int)locationY andTextAreaWidth:(int)textWidth andTextAreaHeight:(int)textHeight{ NSDictionary *attributesDict; NSMutableAttributedString *attString; attributesDict = @{NSForegroundColorAttributeName : color, NSFontAttributeName : font, NSUnderlineStyleAttributeName : [NSNumber numberWithInt:NSUnderlineStyleSingle]}; attString = [[NSMutableAttributedString alloc] initWithString:text attributes:attributesDict]; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, color.CGColor); CGRect rect = CGRectMake(locationX, locationY, textWidth, textHeight); […]

在Swift中更改AttributedText的字体

我有一些在IB中创build的UILabel,这些UILabel都归因于文本。 每个标签的文本包含不同字体大小和颜色的多行。 在运行时,我希望能够改变这些标签的字体名称而不改变现有的字体大小或颜色。 我已经研究,找不到一个简单的方法来实现这一点。 有任何想法吗?

NSTextAlignmentCenter和NSTextAlignmentRight在NSTextTab中是错误的?

任何人都可以请检查我的东西…只是为了确保我不会生气! 我用tabStops创build了一个NSMutableParagraphStyle ,但是他们并没有出现在我期待的地方: float width = self.myLabel.frame.size.width; self.tabStyle.tabStops = @[[[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentRight location:width – 50 options:nil], [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentRight location:width options:nil]]; 然后我创build了一个方便的方法,用两个string的标签位置创build一个属性string: – (NSAttributedString *)tabbedTextWithFirstString:(NSString *)firstString secondString:(NSString *)secondString { NSDictionary *attributes = @{NSFontAttributeName:[UIFont fontWithName:kHSTFontFaceDefault size:14.0]}; NSString *tabbedStr = [NSString stringWithFormat:@"\t%@\t", firstString]; NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:tabbedStr attributes:attributes]; attributes = @{NSFontAttributeName:[UIFont fontWithName:kHSTFontFaceDefaultBold size:14.0]}; [attrStr appendAttributedString:[[NSAttributedString […]

NSAttributedString initWithData:选项:documentAttributes:错误:痛苦地慢

我正在使用这些代码行来从一个简单的HTMLstring中创build一个属性string: NSDictionary *importParams = @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding) }; NSError *error = nil; NSData *stringData = [HTML dataUsingEncoding:NSUTF8StringEncoding] ; NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:stringData options:importParams documentAttributes:NULL error:&error]; 问题在于,即使对于很less的字符来说,真的很慢(在模拟器上几秒钟)。 我不明白其背后的原因,我不想使用第三方库与自定义HTMLparsing器。 有没有其他的方式比HTML更快地创buildHTML文本的属性string?

NSForegroundColorAttributeName不适用于UILabel

我有一个问题,使用“NSForegroundColorAttributeName”更改子string的颜色。 我很困惑,因为同一个子string的其他属性被应用(下划线)。 还尝试使用不推荐的属性“UITextAttributeTextColor”和其他build议的属性“kCTForegroundColorAttributeName”,并获得相同的效果。 我正在编译iOS 7。 NSString *freeText = [NSString stringWithFormat:@"(%@)", self.me.activity.text]; int lblMaxWidth = arrImgView.origin.x – WPRConstraints.BorderOffset; int lblMaxHeight = self.activityView.size.height – WPRConstraints.BorderOffset * 2; RevUILabel *addActivityLbl = [[RevUILabel alloc] initWithFontNameMultiLine:[WPRFonts LattoBold] size:16 sizeConstrain:CGSizeMake(lblMaxWidth,lblMaxHeight)]; addActivityLbl.text = [NSString stringWithFormat:@"%@ %@", Translate(self.me.activity.activityKey), freeText] ; addActivityLbl.textColor = BlackColor; NSMutableAttributedString *str = [addActivityLbl.attributedText mutableCopy]; [str addAttribute:NSUnderlineColorAttributeName value:[UIColor redColor] range:[addActivityLbl.text […]

简单的UIAlertView与NSAttributedString(s)

我正在寻找一个简单的方法来使用一个非常简单的消息框类似于NSAttributedString : NSString *new_item = [NSString stringWithFormat:@"<span style=\"font-family: Helvetica Neue; font-size: 12.0\">%@</span>", @"MOTD HTML String downloaded from website"]; NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[new_item dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MOTD" message:attrStr delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; 我上面的代码需要一个HTML格式的string,已经从服务器上下载,确保文本大小适合屏幕,然后尝试将NSAttributedString发送到UIAlertView 。 但是UIAlertView不喜欢这个。 这个问题最简单的方法是什么?(非HTML格式的MOTD不是一个选项)

NSAttributedString冻结UITableView

使用NSAttributedString滚动时,应用程序真的冻结(当我使用NSString时,它工作正常),所以我的方法: – (void)setSubtitleForCell:(TTTableViewCell *)cell item:(TTPhotoPost *)item { NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData: [item.caption dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil]; [cell.descriptionLabel setAttributedText:attributedString]; } 那里有错误? 或者使att.string更快的方法?

在UILabel中NSAttributedString尾部截断

我使用ContextLabel来parsing@,#和URL的。 这是我find的最好的解决scheme,使其大小正确,不影响性能。 它首先在input处parsingstring并将其转换为NSAttributedString ,然后将其分配给UILabel attributedText NSAttributedString attributedText 。 一切都按预期工作,除了截尾 – 这是非常不正确的(见下图) 我应该从哪里开始挖掘 – 属性string是否属于错误的属性? 或标签布局问题? 谢谢!