Tag: nsmutableattributedstring

多种颜色的文字iOS

我们将实现MutableString来帮助为小部件提供多种颜色的文本 这就是我们所需要的: 让minAttr = NSMutableAttributedString(string:{text})minAttr.addAttribute(NSAttributedStringKey.foregroundColor,value:{color} range:NSMakeRange(0,minAttr.length))attributedText.append(minAttr) 提供可同时处理多种文本颜色的扩展

修剪NSMutableAttributedString中的第一个字符

我正在使用NSMutableAttributedString显示标签中的属性string。 有没有办法修剪NSMutableAttributedString的第一个字符,而不改变属性。

tableviewcell中的归属string在单元出列和重载之前不显示粗体文本

我有一个表格视图,其中的单元格有一些标签文本。 正在从cellForRowAtIndexPath正确设置文本。 正确设置文本的颜色,但粗体属性不会显示,直到单元格出列。 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MyCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier]; Model *model = [self.fetchedResultsController objectAtIndexPath:indexPath]; cell.tag = indexPath.row; [cell updateContentWithModel:model atIndexPath:indexPath]; return cell; } – (void)updateContentWithModel:(Model *)model atIndexPath:(NSIndexPath *)indexPath { self.model = model; [self setTextFromModel]; [self setImageFromModelAtIndexPath:indexPath]; } – (void) setTextFromModel { self.title.text = self.model.header; self.status.attributedText = [self boldString:self.model.status fontSize:self.status.font.pointSize color:[UIColor […]

设置NSLinkAttributeName字体颜色

我觉得我错过了一些容易,但我似乎无法find如何做到这一点: 我将属性设置为如下所示的链接: [myAttrString addAttribute:NSLinkAttributeName value:linkURL range:selectedRange]; 这有效,但链接是蓝色的,我似乎无法改变颜色。 这设置除了链接到白色的一切: [myAttrString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:selectedRange]; 是否有另一个颜色属性名称,我似乎无法find特定于链接?

在iOS 7.0.3中为NSMutableAttributedString崩溃

由于几天,我有一个崩溃,只发生在iOS与下面的代码行 [myAttributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Italic" size:myLabel.font.pointSize] range:rangeOfSubString]; debugging器给出的原因是 “终止应用程序,由于未捕获的exception”NSInvalidArgumentException“,原因:'NSConcreteMutableAttributedString addAttribute:value:range :: nil value'”exceptiontypes:SIGABRT 我从文档中知道它的价值是零。 任何想法为什么[UIFont fontWithName:@“HelveticaNeue-Italic”size:myLabel.font.pointSize]将返回零在iOS 7.0.3? (它在iOS 7.0.2中工作得非常好)

在swift中为iOS创build纯文本(Android格式)文本的属性string

我从Localizable.strings中读取string,其中包含类似于Android应用程序的strings.xml中的基本内容 "testShort" = "A <b>short</b>\ntest with another<b>bold text</b>"; 粗体和换行是我在文本中唯一的两个格式属性。 我正在尝试开发这样的方法,现在几天没有成功: func ConvertText(inputText: String) -> NSAttributedString { // here comes the conversion to a representation with Helvetica 14pt and Helvetica-Bold 14pt including line feeds. } 我最终的目标是在UITextView的属性文本中显示文本。 不熟悉Objective-C的Swift和iOS,我觉得很难做String操作,因为它们非常不同,也很复杂,所有的例子都在Objective-C中。 更难的是大多数API方法在Swift中不可用或不同于Objective-C中的… 这里是我在这里的许多其他职位的帮助下,迄今为止尝试的方法体: var test = inputText.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)! attrStr = NSAttributedString( data: test, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil, error: […]

在iOS8中使用NSMutableAttributedStringstring的下划线不起作用

我尝试在一个string中加下划线,例如' test string 'string中的' string '部分。 我使用NSMutableAttributedString ,我的解决scheme在iOS7上运行良好。 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"test string"]; [attributedString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(5, 6)]; myLabel.attributedText = attributedString; 问题是我的解决scheme不再适用于iOS8 。 花了一个小时testingNSMutableAttributedString多个变种,我发现这个解决scheme只有当范围从0开始(长度可以不同)的作品。 这是什么原因? 我该如何解决这个问题?

更改NSMutableAttributedString中链接的颜色

我有下面的代码,但我的链接总是蓝色的。 我如何将它们的颜色拼成一个? [_string addAttribute:NSLinkAttributeName value:tag range:NSMakeRange(position, length)]; [_string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:(12.0)] range:NSMakeRange(position, length)]; [_string addAttribute:NSStrokeColorAttributeName value:[UIColor greenColor] range:NSMakeRange(position, length)]; _string是一个NSMutableAttributedString,位置和长度工作正常。

使UITextField有一个静态和属性前缀

我想创build一个UITextField ,它有一个静态的前缀,并且用户不能编辑或删除,同时它也被归为浅灰色的字体颜色。 文本字段的可编辑部分应始终以黑色显示。 下面给出一个例子: 它本质上是用于input一个用户名,带有一个不断加前缀的域。 我已经尝试使用textFieldShouldClear和textField:shouldChangeCharactersInRange:replacementString:与NSMutableAttributedString一起委托方法,但根本无法破解它: – (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSMutableAttributedString *text = [textField.attributedText mutableCopy]; [text addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, 7)]; if (textField.text.length > 7) { [text addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(7, textField.attributedText.length – 7)]; } [text addAttribute:NSFontAttributeName value:[UIFont gothamFontForSize:textField.font.pointSize andWeight:kGothamLight] range:NSMakeRange(0, textField.attributedText.length)]; return range.location > 6; } – (BOOL)textFieldShouldClear:(UITextField *)textField { NSMutableAttributedString […]

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,有没有人遇到同样的问题?