Tag: nsattributedstring

如何设置标签高度自动调整阅读更多/更less使用Swift 3?

我想在最后创build带有Read More / Read Less的段落。 这是我的代码; func getLabelHeight(text: String, width: CGFloat, font: UIFont) -> CGFloat { let lbl = UILabel(frame: .zero) lbl.frame.size.width = width lbl.font = font lbl.numberOfLines = 0 lbl.text = text lbl.sizeToFit() lbl.adjustsFontSizeToFitWidth = true return lbl.frame.size.height } @IBAction func btnReadMore(_ sender: Any) { if isLabelAtMaxHeight { btnReadmore.setTitle(NSLocalizedString("Read more", comment: ""), for: .normal) […]

粘贴格式化文本,而不是图像或HTML

我试图模拟iOS页面和Keynote应用程序的粘贴板行为。 简而言之,允许将基本NSAttributedString文本格式(即BIU)粘贴到UITextView中,而不是图像,HTML等等。 行为总结 如果您从Notes应用程序,Evernote或文本和图像中复制来自网站的格式化文本,Pages将只粘贴纯文本string 如果您从Pages或Keynote中复制格式化文本,则会将其格式化的文本粘贴到Pages,Keynote等其他位置。 一个不希望的后果,但也许很重要的是,Notes应用程序或Evernote都不会粘贴从Pages或Keynote复制的格式化文本。 我猜测应用程序之间的差异是使用NSAttributedStrings,而不是HTML? 这是如何完成的? 在Mac OS上,看起来您可以要求粘贴板返回不同types的自身,让它提供丰富的文本和string表示forms,并使用富文本作为首选。 不幸的是,iOS的readObjectsForClasses似乎不存在。 也就是说,我可以通过日志看到iOS有一个RTF相关types的粘贴板, 感谢这篇文章 。 我不能,但是,find一种方法来请求一个NSAttributedString版本的粘贴板内容,所以我可以优先考虑粘贴。 背景 我有一个应用程序,允许基本的NSAttributedString用户可编辑格式(即粗体,斜体,下划线)的文字在UITextViews。 用户想从其他应用程序(例如Safari中的网页,Notes应用程序中的文本)复制文本,以便粘贴到我的应用程序中的UITextView中。 允许粘贴板以默认方式运行意味着我可能最终得到我的应用程序无意处理的背景颜色,图像,字体等。 下面的例子显示了当粘贴到我的应用程序的UITextView中时,如何复制具有背景颜色的文本。 我可以通过inheritanceUITextView来克服1 – (void)paste:(id)sender { UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard]; NSString *string = pasteBoard.string; NSLog(@"Pasteboard string: %@", string); [self insertText:string]; } 意想不到的后果是,失去了保留从我的应用程序中复制的文本格式的能力。 用户可能想从我的应用程序中的一个UITextView复制文本,并粘贴到我的应用程序中的另一个UITextView。 他们会希望保留格式(即大胆,斜体,下划线)。 见解和build议表示赞赏。

更改NSAttributedString的背景颜色

我只是想知道如何改变一个NSAttributedString的背景颜色,我可以看到背景一直是白色的,但是我想把它变成黑色。 那可能吗?? 非常感谢!

为NSAttributedString的不同部分应用不同的属性

我知道可以将属性应用于NSAttributedString。 但是,我们如何将不同的属性应用于相同的属性string 为string“这是一个归因文本”。 我们如何设置一个特定的属性(背景颜色或前景色)为“这是”另一个属性(背景色或前景色)为“属性文本”。 有什么办法来实现这个….? 还有什么办法可以在iOS中设置NSAttributedString的背景颜色?

在后台线程从HTML的NSAttributedString

我需要的是从相对较大的HTMLstring创buildNSAttributedString对象,并将它们(NSAttributedString-s)存储在数据库中。 当然,我想在后台线程中这样做。 这里是一个简单的代码(失败)来演示我正在尝试做什么: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSString *HTMLString = @"<p>HTML string</p>"; NSDictionary *options = @{NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding)}; NSError *error = nil; NSAttributedString *attributedText = [[NSAttributedString alloc] initWithData:[HTMLString dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:nil error:&error]; }); 根据这个讨论 ,根本不可能在后台线程中执行它,因为使用NSHTMLTextDocumentType选项创buildNSHTMLTextDocumentType选项使用WebKit ,它需要在执行任何asynchronous工作时旋转runloop。 但可能是别人想通了? 我有非常简单的HTML,只是文本和链接,可能有一种方法来指定,从HTML创build一个NSAttributedString不会需要任何“ WebKit -rendering”? 或者可能有人可以推荐第三方库从简单的HTML不使用WebKit创buildNSAttributedString ?

NSAttributedStringalignment不能在html内容上工作

想要更改html标签的alignment方式。 没有任何工作 我没有看到HTML中的任何CSS。 没有更改设置的设置。 我也直接在UILabel上设置左alignment。 我错过了什么? 代码是UILabel扩展名。 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; paragraphStyle.alignment = NSTextAlignmentLeft; NSDictionary *attr = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding), NSParagraphStyleAttributeName: paragraphStyle}; NSError *error = nil; NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]initWithData:[html dataUsingEncoding:NSUTF8StringEncoding] options:attr documentAttributes:nil error:&error]; // also tried with this [attributedText addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, html.length) ]; self.attributedText = attributedText; […]

iOS中的NSAttributedString中NSBackgroundColorAttributeName-like属性?

我正在计划使用NSAttributedString来突出部分string与用户search的匹配查询。 但是,我找不到一个iOS的等价物NSBackgroundColorAttributeName – 没有kCTBackgroundColorAttributeName 。 这样的事情是否存在,类似于NSForegroundColorAttributeName成为kCTForegroundColorAttributeName的方式?

为什么使用HTMLstring对NSAttributedString的初始调用比后续调用要长100倍?

我需要在我的iOS应用程序中显示HTML文本。 我决定我将使用NSAttributedString , initWithData:options:documentAttributes:error:的内置方法。 实际的parsing工作很好,但是,我似乎遇到了一个非常奇怪的错误,只有似乎表明自己,如果我有debugging器附加。 这种方法第一次被调用,运行iOS 7.0.4的iPhone 5S运行时间仅为1秒,iPod Touch 5代运行时间约为1.5秒。 模拟器上也出现了这个怪癖,但由于模拟器的速度很快,所以这个问题显而易见。 接下来的通话只需要10-50ms左右,比起初始通话要快得多。 这似乎与inputstring的caching没有关系,因为我在“真实”应用程序中用多个inputstringtesting了它。 但是,当我运行程序没有debugging器,它按预期运行,需要约10-20毫秒,这是我期望的HTMLparsing。 这是代码的相关部分: -(void) benchmarkMe:(id)sender { NSData *data = [testString dataUsingEncoding:NSUTF8StringEncoding]; NSTimeInterval startTime = [[NSDate date] timeIntervalSinceReferenceDate]; // So the complier doesn't keep complaining at me. __attribute__((unused)) NSAttributedString *parsed = [[NSAttributedString alloc] initWithData:data options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding) } documentAttributes:nil error:nil]; NSTimeInterval endTime […]

Objective-C中Android的“Spannable”相当于什么

我在一个iOS应用程序,应该能够突出文本,并使其可点击。 我在iOS中阅读了有关NSAttributedString的内容 ,但它比Android中的Spannable更复杂。 如果没有,还有没有其他的Objective c方法可以做到这一点? 我应该怎么做,使用NSAttributedString单词突出显示一个段落,以及如何使我的文本点击。 更新: 我想要什么,每个单词应该是可点击的,并且可以在一个段落中突出显示为单个单词 。

如何在NSUserDefaults中使用存储和使用NSMutableAttributedString

我想创build一个属性string,然后将其存储在NSUserDefaults ,然后再次访问它并将属性string分配给NSUserDefaults 。 我如何去做这件事? 提前致谢。 我不知道很多目标c,所以我不能提及这个答案