Tag: nsattributedstring

NSMutableAttributedString的属性NSStrikethroughStyleAttributeName在iOS8中无法正常工作

我有一个可变的归属string没有NSStrikethroughStyleAttributeName属性是这样的: NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc] initWithString:@"aaaa" attributes:@{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]}]; 另一个带有NSStrikethroughStyleAttributeName属性的可变属性string,如下所示: NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc] initWithString:@"bbbb"]; 和一个包含两个string的整个string: NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] init]; [labelString appendAttributedString:str1]; [labelString appendAttributedString:str2]; 当我把整个string附加到UILabel时: _label.attributedText = labelString; 它在iOS7和iOS8中都很好地显示,如下所示: AAAA BBBB 但是当我交换他们的位置时: [labelString appendAttributedString:str2]; [labelString appendAttributedString:str1]; 它在iOS7中正确显示,但在iOS8中不正确 ios7:bbbb AAAA ios8:bbbbaaaa 看来,在iOS8中,如果属性string中的第一个字符未被清除,则UILabel不会正确地显示删除线。 我认为这是iOS8的一个bug,有没有人遇到同样的问题?

计算字体大小以适应框架 – 核心文本 – NSAttributedString – iOS

我有一些文本,我正在通过NSAttributedString(下面的代码)绘制到一个固定的框架。 目前我很难将文字大小编码为16.我的问题是,有没有一种方法可以计算给定帧的文本的最佳拟合大小? – (void)drawText:(CGContextRef)contextP startX:(float)x startY:(float) y withText:(NSString *)standString { CGContextTranslateCTM(contextP, 0, (bottom-top)*2); CGContextScaleCTM(contextP, 1.0, -1.0); CGRect frameText = CGRectMake(1, 0, (right-left)*2, (bottom-top)*2); NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithString:standString]; [attrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:16.0] range:NSMakeRange(0, attrString.length)]; CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)(attrString)); struct CGPath * p = CGPathCreateMutable(); CGPathAddRect(p, NULL, frameText); CTFrameRef frame = CTFramesetterCreateFrame(framesetter, […]

文本中心alignment

我已经尝试了一切,但似乎无法居中这个文本。 有人可以告诉我错误在哪里。 NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new; paragraphStyle.alignment = NSTextAlignmentCenter; label.attributedText = [[NSAttributedString alloc] initWithString:cell.EventTitle.text attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor],NSParagraphStyleAttributeName:paragraphStyle,NSBaselineOffsetAttributeName : @0,NSFontAttributeName : [UIFont fontWithName:@"BrandonGrotesque-Black" size:34]}];

防止部分NSAttributedString中的换行符

我正在使用一个UILabel ,它具有大的主要文本,然后是较小的文本,告诉你谁说的: 现在它基本上是一个NSAttributedString ,在小文本上有一个字体属性。 我想设置的东西,所以大文本包装,但小文本没有。 也就是说,如果文本将与正确的项目相同,则它应该按原样呈现,但是它会像在左侧项目中那样进行包装,整个小文本应该出现在下一行: 我试图达到的HTML等价物是: Title <nobr>Subtitle</nobr> – or – Title <span style="white-space:nowrap">Subtitle</span> 我试过用NSHTMLTextDocumentType将这两个转换为NSHTMLTextDocumentType ,它似乎没有做一个直接的翻译。

Markdown NSAttributedString库吗?

既然NSAttributedString在iOS 6中完全受支持,那么是否有一个库将使用降价的NSString并将其转换为NSAttributedString?

replaceNSAttributedString中的整个文本string,而不修改其他属性

我有一个NSAttributedString的引用,我想改变属性string的文本。 我想我必须创build一个新的NSAttributedString和更新引用这个新的string。 但是,当我这样做,我失去了以前的string的属性。 NSAttributedString *newString = [[NSAttributedString alloc] initWithString:text]; [self setAttributedText:newString]; 我在self.attributedText引用了旧的归因string。 我怎样才能保留以前的新string中的属性?

什么是iOS 7中的NSLineBreakMode等效的string绘制方法?

有一种方法 – (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode alignment:(NSTextAlignment)alignment; 我现在必须replaceiOS 7 NSDictionary *attributes = @{NSFontAttributeName: font}; [self drawInRect:rect withAttributes:attributes]; 但如何指定像换行一样的换行符模式? 我search了Attributedstring绘图符号的文档,但没有提到换行符模式。 这是自动的总是自动换行吗?

在UILabel.attributedText *不*蓝色和*不*下划线的链接

我想在我的OHAttributedLabel中的一些词是链接,但我希望他们是蓝色以外的颜色,我不想下划线。 这给了我一个带下划线的文字的蓝色链接: -(void)createLinkFromWord:(NSString*)word withColor:(UIColor*)color atRange:(NSRange)range{ NSMutableAttributedString* mutableAttributedText = [self.label.attributedText mutableCopy]; [mutableAttributedText beginEditing]; [mutableAttributedText addAttribute:kOHLinkAttributeName value:[NSURL URLWithString:@"http://www.somewhere.net"] range:range]; [mutableAttributedText addAttribute:(id)kCTForegroundColorAttributeName value:color range:range]; [mutableAttributedText addAttribute:(id)kCTUnderlineStyleAttributeName value:[NSNumber numberWithInt:kCTUnderlineStyleNone] range:range]; [mutableAttributedText endEditing]; self.label.attributedText = mutableAttributedText; } 由于我使用的是OHAttributedLabel,我也尝试过使用NSAttributedString+Attributes.h类中的方法,但是那些返回蓝色下划线的链接也是如此: -(void)createLinkFromWord:(NSString*)word withColor:(UIColor*)color atRange:(NSRange)range{ NSMutableAttributedString* mutableAttributedText = [self.label.attributedText mutableCopy]; [mutableAttributedText setLink:[NSURL URLWithString:@"http://www.somewhere.net"] range:range]; [mutableAttributedText setTextColor:color range:range]; [mutableAttributedText setTextUnderlineStyle:kCTUnderlineStyleNone range:range]; self.label.attributedText = mutableAttributedText; } […]

将UITextViews文本replace为属性string

我有一个UITextView ,当用户input文本时,我想要dynamic地格式化文本。 像语法突出显示… 为此,我想使用UITextView … 一切工作正常期待一个问题:我从文本视图中的文本,并从它做一个NSAttributedString 。 我对这个属性string进行了一些编辑,并将其设置为textView.attributedText 。 每当用户键入时都会发生这种情况。 因此,我必须在编辑到attributedText selectedTextRange之前记住selectedTextRange然后将其设置回来,以便用户可以在之前input的地方继续input。 唯一的问题是,一旦文本足够长,需要滚动,如果我input缓慢, UITextView现在将开始滚动到顶部。 这是一些示例代码: – (void)formatTextInTextView:(UITextView *)textView { NSRange selectedRange = textView.selectedRange; NSString *text = textView.text; // This will give me an attributedString with the base text-style NSMutableAttributedString *attributedString = [self attributedStringFromString:text]; NSError *error = nil; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"#(\\w+)" options:0 error:&error]; NSArray […]

更改NSAttributedString中子string的属性

这个问题可能是这个问题的重复。 但是答案不适合我,我想要更具体。 我有一个NSString ,但我需要一个NS(Mutable)AttributedString和这个string中的一些单词应给予不同的颜色。 我试过这个: NSString *text = @"This is the text and i want to replace something"; NSDictionary *attributes = @ {NSForegroundColorAttributeName : [UIColor redColor]}; NSMutableAttributedString *subString = [[NSMutableAttributedString alloc] initWithString:@"AND" attributes:attributes]; NSMutableAttributedString *newText = [[NSMutableAttributedString alloc] initWithString:text]; newText = [[newText mutableString] stringByReplacingOccurrencesOfString:@"and" withString:[subString mutableString]]; “和”应该是大写的红色。 该文件说,mutableString保持属性映射。 但是用我的替代品,我在赋值的右侧(在我的代码片段的最后一行)没有更多的attributesString。 我怎样才能得到我想要的? ;)