我想将包含RTFD的NSAttributedString转换为大写,而不会丢失现有字符和图形的属性.
谢谢,
我想略微修改一个角色的y位置NSAttributedString.我知道我可以使用属性字典下标(或上标):
@{(id)kCTSuperscriptAttributeName: @(-1)}
Run Code Online (Sandbox Code Playgroud)
不幸的是,由此引起的转变在我的情况下太大了.我正在寻找一个以1点为单位调整基线的选项.
属性字符串将显示在UIButton使用中-setAttributedTitle:forState:.
如果你在一个中包含Unicode字符NSString,它们中的很多将采用该文本的颜色集 - 它们只是该字体的常规字形,因此它们像任何其他字符一样显示.但是有一些着色的Unicode字符,例如GLOBE WITH MERIDIANS,它是带阴影的蓝色渐变.但我在其他地方看到了这个相同的字形,这是一个没有阴影的简单黑色轮廓,例如在iOS键盘中.我想使用那个字形,但没有装饰,也不必创建和使用图像.我想知道不同的字体是否会以不同的格式呈现它,而iOSFonts.com确实显示不同的样式(粗体,斜体),它们都是蓝色的.是否可以获得简单的普通版本?
当然有可能,因为这似乎正是Apple用Tip实现的.请注意,globe与文本颜色完全相同,并且它与所有其他字符一起包含在字符串中.当然那不是一个UIImage?

不同字体的字符:

编辑:链接问题中提供的解决方案不适用于此字符,因为变体字符看起来与原始字符完全相同 - 带阴影的蓝色.
我有一个自定义的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) 在iOS 8(或甚至可能在iOS 7/7.1中)NSAttributedStringiOS上添加了渲染文本表的功能.此API 在OS X上公开,但在iOS 上不公开.但是,可以通过将包含表的HTML传递给iOS,在iOS上创建包含文本表的属性字符串-[NSAttributedString initWithData:options:documentAttributes:error:].
正确创建了属性字符串,但是当我尝试使用a渲染字符串时UITextView,会抛出此异常:
<NSATSTypesetter: 0x7f8900faec90>: Exception *** -[NSConcreteTextStorage attribute:atIndex:effectiveRange:]: Range or index out of bounds raised during typesetting layout manager <NSLayoutManager: 0x7f8902e13440>
1 containers, text backing has 1884 characters
Currently holding 1884 glyphs.
Glyph tree contents: 1884 characters, 1884 glyphs, 1 nodes, 64 node bytes, 7680 storage bytes, 7744 total bytes, 4.11 bytes per character, 4.11 bytes per glyph
Layout tree contents: 1884 …Run Code Online (Sandbox Code Playgroud) 假设我有一个AttributedString:"已经有一个帐户?登录!".我将此字符串放在UILabel中.现在,当用户点击"登录!"时,当前的viewController应该转到另一个viewController,或者在点击登录时调用某个函数.
任何代码或建议都应该没问题.
我发现字符串着色的时间取决于使用了多少不同的NSColors.在下面的代码中,如果我只对三种情况使用一种颜色,则文本着色过程比三种不同颜色用于这三种情况的情况快3倍,每种情况下每种颜色.为什么?有没有办法不减慢三种不同颜色的着色?
for i in 0..<arrayOfNSRangesForA.count
{
textFromStorage.addAttribute(NSForegroundColorAttributeName, value: NSColor.green, range: arrayOfNSRangesForA[i])
}
for i in 0..<arrayOfNSRangesForT.count
{
textFromStorage.addAttribute(NSForegroundColorAttributeName, value: NSColor.green, range: arrayOfNSRangesForT[i])
}
for i in 0..<arrayOfNSRangesForC.count
{
textFromStorage.addAttribute(NSForegroundColorAttributeName, value: NSColor.green, range: arrayOfNSRangesForC[i])
}
Run Code Online (Sandbox Code Playgroud)
更新
我发现了一件坏事.当我改变着色时NSForegroundColorAttributeName,NSBackgroundColorAttributeName运行时间显着增加--10倍.对于20 000个字符,它是一种颜色,NSForegroundColorAttributeName- 1秒,NSBackgroundColorAttributeName- 10秒; 如果有三种颜色 - 相应的3和30秒.对我而言,Swift的功能非常糟糕!由于DNA的长度是数千个A,T,G,C字符,因此无法用DNA(ATGC序列)着色进行正常工作!
更新 在评论中,我建议仅为文本的可见部分着色.我尝试过这种方法,与标准方式相比,即使对于较短的文本也是如此.所以,我有文本的NSRange文本的可见部分,并在滚动时使用通知滚动时在飞行中着色.这是一个糟糕的方式.
我试图用属性String替换子字符串.以下是我的代码.
let searchText = self.searchBar.text!
let name = item.firstName ?? ""
let idNo = "Employee Id. \(item.employeeId ?? "NA")"
if let range = name.range(of: searchText, options: String.CompareOptions.caseInsensitive, range: nil, locale: nil)
{
let attributedSubString = NSAttributedString.init(string: name.substring(with: range), attributes: [NSFontAttributeName : UIFont.boldSystemFont(ofSize: 17)])
let normalNameString = NSMutableAttributedString.init(string: name)
normalNameString.mutableString.replacingCharacters(in: range, with: attributedSubString)
cell.name.attributedText = normalNameString
}
else
{
cell.name.text = name
}
Run Code Online (Sandbox Code Playgroud)
我收到编译时错误:
"无法将'Range'类型的值(又称'Range')转换为预期的参数类型'NSRange'(又名'_NSRange')".
我应该在这里改变什么?
iOS 11在导航栏中引入了大文本选项.我想要一个使用多种颜色的标题.例如:
设置标题相当容易,甚至可以更改整个标题的颜色:
[[self navigationItem] setTitle: @"Colors"];
[[[self navigationController] navigationBar] setLargeTitleTextAttributes: @{NSForegroundColorAttributeName: [UIColor colorFromHex: redColor]}];
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚的是如何改变标题的一部分.例如,一种选择这样的范围的方法 - NSRangeMake(0, 1)- 以便我可以为它应用颜色.
这一定是可能的,对吧?
我正在尝试将属性文本转换为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)