Tag: nsattributedstring

Swift中的归因字符串:正确的方法

我们如何使可可少一些冗长,类型多一些安全。 当我在2001年晚些时候开始使用Cocoa时,我是从Java方面的经验和少量的纯C语言中学到的。Cocoa简直太了不起了(请查阅Ars Technica的这篇很酷的老文章),以及与旧Mac的区别。工具箱很大。 一个崭新的世界即将来临,我为所有机会提供了一个如此完整的,功能齐全的框架,这让我感到非常兴奋。 自从这些天以来,已经过去了很多年,古老的NeXT遗产是从iOS和UIKit继承而来的,我们也经历了移动革命,尤其是得益于完整的解决方案,该解决方案确实为我们所谓的应用经济创造了肥沃的环境( ,当然是在macOS上启动的。 使用Swift,就像时光倒流,再次体验相同的体验。 即使有这个新机会,UIKit(以及程度较小的AppKit)也正在经历向新生活的平稳过渡(虽然我的梦想是关于新的重写框架,但我可以理解选择平滑过渡的原因)。 Swift标准库中的基金会)。 因此,尽管我们仍在与这种过渡作斗争,但无论如何我们都可以使我们的生活更轻松。 NSAttributedString是我在后Swift时代开始讨厌的事情之一; 创建,组合样式并将其应用到字符串,然后将其渲染并不是一件困难的事情,但是它很无聊,冗长且令人讨厌。 就像不久前通过代码使用Auto Layout一样; 我们应该得到更好的体验,特别是现在我们正在使用Swift:我们可以使其变得类型安全,易于使用和简洁。 因此,从这种经验开始,我创建了一个名为SwiftRichString的小型库(原始名称,您认为不是吗?)。 类型安全 我要解决的第一个问题是关于类型安全的问题。 Swift实现类型安全 ,这基本上意味着您不能对变量的类型术语不恰当地使用它。 让开发环境强制执行类型安全是处理这些问题的最安全方法。 NSAttributedStrings使用NSDictionary设置文本属性; 属性标识符(作为字符串)及其值一起传递。 像那样: 为了使其更安全,我引入了样式的概念。 您可以使用名称创建样式(我们将在后面讨论),并使用传统的创建模式分配属性: 甚至font属性也是类型安全的; 您可以通过指定iOS提供的一种字体,以类型安全的方式创建UIFont对象。 FontAttribute也可以扩展,您可以添加自己的字体名称(或者,如果您很懒,可以通过指定文字以旧的方式实例化字体)。 通过查看库,您会发现其他一些特殊的结构,例如StrikeAttribute,ShadowAttribute或UnderlineAttribute。 所有这些都用于使事情变得更清洁,更快捷地使用(请查看文档以获取完整列表)。 样式的理想方案是创建将在下一个应用程序的代码中使用的一组样式。 只有一种特殊的样式称为.default。 无论将默认样式传递给函数的styles参数的顺序如何,默认样式都将首先应用。 这样可以确保您的字符串具有基本的通用样式,并可以在其上轻松添加一个或多个属性。 如果您不需要在代码中使用带标签的字符串,则可以创建Style对象而无需给它们命名。 只需在set()函数中使用它即可。 无痛属性字符串的创建和管理 创建NSAttributedString,将它们结合起来并应用样式既乏味又冗长; 大量无聊的代码使完成一项相当容易的任务。 让我向您展示一个非常简单的示例。 文章继续对我不利。 点击此处以继续阅读!

将NSAttributedstring(与NSTextAttachment有图像)转换为HTML在ios obj c

我知道如何将常见的NSAttributedString (没有图像)转换成HTML。 我将图像设置为NSTextAttachment NSAttributedString 。 我怎么能把整个属性string转换成HTML? UIImage *image = [UIImage imageNamed:@"Test.png"]; NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init]; textAttachment.image = image; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:htmlString]; NSAttributedString *attstringImage = [NSAttributedString attributedStringWithAttachment:textAttachment]; [attributedString appendAttributedString:attstringImage]; NSAttributedString *titleContent = (NSAttributedString *)attributedString; NSDictionary *documentAttributes = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}; NSData *htmlData = [titleContent dataFromRange:NSMakeRange(0, titleContent.length) documentAttributes:documentAttributes error:NULL]; NSString *html = [[NSString […]

如何在标签iOS中显示自定义Emojis Url链接

如何在标签中显示表情符号链接。 表情符号Unicode格式正确显示,但是当我们显示emojis url链接时,它不显示其显示相同的URL链接。 我使用这个代码来显示Emojis: – NSString * html = msCommentObj.mCommentText; NSAttributedString *attr = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)} documentAttributes:nil error:nil]; NSString *finalString = [attr string]; descriptionCommentsLbl.text = finalString;* 这是Emojis回应: – 是的。 https://mysponsers.com//img/emoticons/47.png https://mysponsers.com//img/emoticons/41.png

NSAttributedString高度由width和numberOfLines限制

我需要计算不使用UILabel的sizeThatFits方法的自定义标签中的文本矩形。 下面的代码不能正常工作。 主要思想是在index = numberOfLines – 1处查找CTLine,并返回其最大y位置。 但是,结果文本高度有时太大,有时不足以画出最后一行。 – (CGSize)fittingSizeWithSize:(CGSize)size numberOfLines:(NSInteger)numberOfLines { if (numberOfLines == 0) { return [self fittingSizeWithSize:size]; } CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)self); if (framesetter == NULL) { return CGSizeZero; } CGPathRef path = CGPathCreateWithRect(CGRectMake(0,0,size.width,size.height), NULL); CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, self.length), path, NULL); NSArray *lines = (NSArray *) CTFrameGetLines(frame); if (lines.count […]

在Swift中,我怎样才能改变一部分string的属性?

我正试图用一些文字显示分数。 分数显示在一个句子的中间,我希望字体的分数大于文本的其余部分。 我的代码如下: let fontSizeAttribute = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 43)] let myString = String(describing: Int(finalScore!.rounded(toPlaces: 0))) let attributedString = NSAttributedString(string: myString, attributes: fontSizeAttribute) scoreLabel.text = "Your score is \(attributedString)%, which is much higher than most people." 我看不出这个实现有什么问题,但是当我运行它时,它说:“你的分数是9 {NSFont =”UITCFont:0x7f815 … 我觉得我正在做一些愚蠢的事情,但不知道它是什么。 任何帮助,将不胜感激!

用更多的时间来显示带有自定义字体的NSAttributedstring

我正在使用核心文本明智地显示NSAttributedstring列。 它工作正常。 使用系统字体时,在模拟器和设备中都不会显示任何延迟。 但是使用自定义字体时,会花费更多的时间来显示设备中的内容。 但在模拟器中,结果很快。 – (void)updateAttributedString { // Existing Code if (self.text != nil) { self.attributedString = [[NSMutableAttributedString alloc] initWithString:self.text]; NSRange range = NSMakeRange(0, [self.text length]); // Regarding Fixed font // [ self.attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"TAUN_Elango_Abirami" size:20] range:range];//This is my custom font // Regarding custom Font using below code if (self.font != nil) { […]

Swift 4标签属性

我从swift快速移动到swift 4.我有UILabels,我给非常具体的文本属性的标签。 当我正在初始化strokeTextAttributes的时候,我得到了“意外地发现了零,同时展开可选值”的错误。 我完全失去坦率。 在swift 3中,strokeTextAttributes是[String:Any],但是Swift 4抛出错误,直到我将它改变为下面的内容。 let strokeTextAttributes = [ NSAttributedStringKey.strokeColor.rawValue : UIColor.black, NSAttributedStringKey.foregroundColor : UIColor.white, NSAttributedStringKey.strokeWidth : -2.0, NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 18) ] as! [NSAttributedStringKey : Any] chevronRightLabel.attributedText = NSMutableAttributedString(string: "0", attributes: strokeTextAttributes)

如何将HTML映射到iOS中的NSAttributedString

由于NSAttributedString initWithHTML addtion只能在Mac OS上使用,而不能在iOS上使用。

系统字体中没有字形时如何下标字母?

我已经能够成功地在我的应用程序中下标数字: static const unichar kSubscriptZero = 0x2080; int numberOfHydrogens = 2; NSString *header1 = [NSString stringWithFormat:@"H%CO", kSubscriptZero + numberOfHydrogens]; 问题是这样的:我试图用unicode字符中的其他字符做同样的事情,但它不工作,我得到的只是一个方框而不是我的字符,事实certificate,这意味着我的字形没有该字符的字体。 我回顾了以前在这里问过的一个问题,并尝试了其他字符,而不是数字: if (section == whicheverSectionIndexIsCorrect) { NSString *plainText = @"2H2 + O2 → 2H2O"; id subscriptOffset = @(-0.5); // random guess here, adjust offset as needed NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:plainText]; // apply attributes […]

如何从NSAttributedstring中分离属性并将这些属性应用于其他string?

例如。 就像我们有一个NSAttributedstring一样,我们需要将string和属性分开,然后在其他长度相同的string上使用这些属性。