标签: nsattributedstring

使用Core Text设置给定字符串范围的背景颜色/突出显示颜色

我在我的iPhone应用程序中使用Core Text布局了一些文本.我正在使用NSAttributedString在给定范围的文本中设置某些样式.

我似乎找不到用于设置背景/高亮颜色的属性,尽管看起来它是可能的.我找不到一个听起来相关的属性名称常量,文档只列出:

kCTCharacterShapeAttributeName
kCTFontAttributeName
kCTKernAttributeName
kCTLigatureAttributeName
kCTForegroundColorAttributeName
kCTForegroundColorFromContextAttributeName
kCTParagraphStyleAttributeName
kCTStrokeWidthAttributeName
kCTStrokeColorAttributeName
kCTSuperscriptAttributeName
kCTUnderlineColorAttributeName
kCTUnderlineStyleAttributeName
kCTVerticalFormsAttributeName
kCTGlyphInfoAttributeName
kCTRunDelegateAttributeName
Run Code Online (Sandbox Code Playgroud)

Twitterrific的开发者Craig Hockenberry在Twitter上公开表示他使用Core Text来呈现推文,Twitterrific有这个背景/亮点,当你触摸链接时我正在讨论.

替代文字

任何正确方向的帮助或指示都会非常棒,谢谢.

编辑:这里是推文链接克雷格发布了提到"核心文本,归因于字符串和大量努力工作",以及后续提到使用CTFrameSetter指标来计算触摸是否与链接相交.

iphone highlight nsattributedstring core-text ios

8
推荐指数
1
解决办法
4265
查看次数

如何在NSAttributedString中将字符大小写更改为Upper

我想将包含RTFD的NSAttributedString转换为大写,而不会丢失现有字符和图形的属性.

谢谢,

cocoa character nsattributedstring uppercase

8
推荐指数
1
解决办法
4790
查看次数

使用CoreText显示NSAttributedString

我听说我可以使用CoreText显示NSAttributedString,谁能说我怎么样(最简单的方法)?

请不要使用CATextLayer或OHAttributedLabel回答.

我知道在这个论坛上有很多关于这方面的问题,但我找不到答案

谢谢!!

iphone nsattributedstring core-text ios

8
推荐指数
2
解决办法
6623
查看次数

最后一行NSAttributedString未在UILabel中呈现

我有文本,其中包含我试图在视图上呈现的HTML格式.目前我正在使用一个NSAtributedString来显示一个文本UILabel.

我获得了如下的属性字符串:

NSString *htmlString = @"<html>"
    "  <head>"
    "    <style type='text/css'>"
    "      body { font: 12pt 'Helvetica'; color: #111111; }"
    "    </style>"
    "  </head>"
    "  <body>";
    htmlString = [htmlString stringByAppendingString:self.descriptionText];
    htmlString = [htmlString stringByAppendingString:@"</body></html>"];

    NSError *err = nil;
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithData: [htmlString dataUsingEncoding:NSUTF8StringEncoding]
                                                                          options: @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}
                                                               documentAttributes: nil
                                                                            error: &err];
Run Code Online (Sandbox Code Playgroud)

然后将其分配给我的标签attributedText属性.self.descriptionText属性NSString可以包含简单的HTML标记,例如带有内联样式的p标记或span标记.在另一个StackOverflow帖子中找到了将字符串包装在HTML文档中的技术.

UILabel被设定为行= 0和换行符=自动换行.我看到的行为是标签正确生长以容纳整个字符串,但无论字符串有多长,字符串的最后一行都不会呈现.

这是一个带有两个标签的屏幕截图.顶部标签(带黄色背景)中包含我的HTML属性字符串.您可以看到标签的大小适合允许3行文本,但只有两行被渲染,其余的被截断.第二个标签(带有红色背景)已经为其Text属性分配了一个纯字符串,它绘制得很好.

示例图像中使用的HTML字符串包含以下文本:

<p>With the world&#39;s longest fan-vaulted ceiling, an original …
Run Code Online (Sandbox Code Playgroud)

html objective-c nsattributedstring uilabel ios

8
推荐指数
2
解决办法
2658
查看次数

如何获得简单的'globe'Unicode字符

如果你在一个中包含Unicode字符NSString,它们中的很多将采用该文本的颜色集 - 它们只是该字体的常规字形,因此它们像任何其他字符一样显示.但是有一些着色的Unicode字符,例如GLOBE WITH MERIDIANS,它是带阴影的蓝色渐变.但我在其他地方看到了这个相同的字形,这是一个没有阴影的简单黑色轮廓,例如在iOS键盘中.我想使用那个字形,但没有装饰,也不必创建和使用图像.我想知道不同的字体是否会以不同的格式呈现它,而iOSFonts.com确实显示不同的样式(粗体,斜体),它们都是蓝色的.是否可以获得简单的普通版本?

当然有可能,因为这似乎正是Apple用Tip实现的.请注意,globe与文本颜色完全相同,并且它与所有其他字符一起包含在字符串中.当然那不是一个UIImage

截图

不同字体的字符:
在此输入图像描述

编辑:链接问题中提供的解决方案不适用于此字符,因为变体字符看起来与原始字符完全相同 - 带阴影的蓝色.

unicode objective-c nsstring nsattributedstring ios

8
推荐指数
1
解决办法
1629
查看次数

设置后,UITextView属性文本为null

我有一个自定义的UICollectionViewCell与一个不可编辑的,不可选择的UITextView.在cellForItemAtIndexPath:方法中,我设置了UITextView的attributedText属性,它显示了所有属性.但是,当我稍后尝试访问该属性文本时(在点击手势识别器的操作方法中),该值为null.

这是我的代码的样子:

查看控制器:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
     TNCommentCollectionViewCell *commentCell = [collectionView dequeueReusableCellWithReuseIdentifier:kCommentCellIdentifier 
                                                                                          forIndexPath:indexPath];

     TNComment *comment = [self.comments objectAtIndex:indexPath.row];
     commentCell.textView.attributedText = [self commentStringForComment:comment];

     return commentCell;
}

- (NSAttributedString *)commentStringForComment:(DAFeedComment *)comment
{
    NSString *usernameString = [NSString stringWithFormat:@"@%@", comment.username];
    NSDictionary *usernameAttributes = @{ NSForegroundColorAttributeName : [UIColor usernameColor],
          NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:14.0f] };
    NSAttributedString *attributedUsernameString = [[NSAttributedString alloc] initWithString:usernameString attributes:usernameAttributes];
    NSMutableAttributedString *labelString = [attributedUsernameString mutableCopy];

    NSDictionary *commentAttributes = @{ NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:14.0f] };
    [labelString appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]]; …
Run Code Online (Sandbox Code Playgroud)

uitextview nsattributedstring ios

8
推荐指数
1
解决办法
1657
查看次数

如何检查UILabel是否以编程方式归因于文本或普通文本?

有没有办法判断UILabel是否使用label.attributedTextlabel.text属性设置了文本?

问题是当你设置时attributedText,text也会更新,反之亦然,因此无法检查nil的这些属性.

nsattributedstring uilabel ios

8
推荐指数
2
解决办法
2596
查看次数

Swift - NSMutableAttributedString更改UITextView中的所有文本

下载我创建的示例

示例链接

我正在添加一个链接到我的UITextView,如下所示:

 let systemFont = self.text.font!
 let linkAttributes = [
          NSFontAttributeName : systemFont,
          NSLinkAttributeName: NSURL(string: alertController.textFields![0].text!)!] as [String : Any]

 let myAttributes2 = [ NSForegroundColorAttributeName: customGreen]

 let attributedString = NSMutableAttributedString(string: "\(urlName)")

 // Set the 'click here' substring to be the link
 attributedString.setAttributes(linkAttributes, range: NSMakeRange(0, urlName.characters.count))//(0, urlName.characters.count))

 self.text.linkTextAttributes = myAttributes2
 self.text.textStorage.insert(attributedString, at: self.text.selectedRange.location)

 let cursor = NSRange(location: self.text.selectedRange.location + "\(urlName)".characters.count, length: 0)
 self.text.selectedRange = cursor
 // self.text.font = systemFont
Run Code Online (Sandbox Code Playgroud)

但插入后,它会将UITextView中的所有当前文本更改为相同的字体.

因此,例如,如果我有一些粗体文本和一些更多的文本是Italic,在我添加url(它是系统字体)后,它会将所有粗体/斜体文本重置为系统字体....

如果有人知道如何保留以前文本的当前字体,我真的很感激帮助.

如需进一步说明,请在下方发表评论!

非常感谢任何有帮助的人!

更新

我正在更改textDidChange中的文本

  if text == " …
Run Code Online (Sandbox Code Playgroud)

uitextview nsattributedstring ios swift

8
推荐指数
1
解决办法
2730
查看次数

使用swift 4的NSAttributedStringKey错误

更新到Swift 4后,我收到此代码的错误

    attributes["NSFont"] = font
    attributes["NSColor"] = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1)
Run Code Online (Sandbox Code Playgroud)

无法使用索引类型为"String"的类型'[NSAttributedStringKey:Any]'下标值

我能够通过替换修复第一行["NSFont"],NSAttributedStringKey.font但我不知道如何修复第二行.

nsattributedstring ios swift swift4 nsattributedstringkey

8
推荐指数
2
解决办法
5358
查看次数

在Swift 4中将属性文本转换为HTML

我正在尝试将属性文本转换为Swift 4中的HTML,以便它可以存储在Firebase/Firestore中并在不同的设备/平台上同步.我已经阅读了我在Stackoverflow上可以找到的每篇文章,包括转换属性字符串,"简单"标记的html, 将属性文本转换为HTML以及此博客文章:http://sketchytech.blogspot.com/2016/02/swift-from -html-to-nsattributedtext-and.html.我发现Swift 4.x在各种代码示例中抛出了NSDocumentTypeDocumentAttribute和NSHTMLTextDocumentType的未解析标识符错误(例如下面的例子).Swift 3.x中没有抛出这些错误.Xcode建议我可能意味着使用NSDocumentTypeDocumentOption但不提供修复,我不确定这是否是我想要的或如果它是如何使用它.

let myAttributes = [ NSAttributedStringKey.foregroundColor: UIColor.green ]
let attrString = NSAttributedString(string: "Hello.", attributes: myAttributes)
let x = attrString
var resultHtmlText = ""
do {

    let r = NSRange(location: 0, length: x.length)
    let att = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]

    let d = try x.data(from: r, documentAttributes: att)

    if let h = String(data: d, encoding: .utf8) {
        resultHtmlText = h
    }
}
catch {
    print("utterly failed to convert to html!!! \n>\(x)<\n") …
Run Code Online (Sandbox Code Playgroud)

html nsattributedstring ios swift

8
推荐指数
1
解决办法
2317
查看次数