我遇到的问题是我希望能够在TextView中更改某些文本的textColor.我使用的是串联字符串,只是希望我附加到TextView文本中的字符串.似乎我想要使用的是NSMutableAttributedString,但我没有找到任何有关如何在Swift中使用它的资源.到目前为止我所拥有的是这样的:
let string = "A \(stringOne) with \(stringTwo)"
var attributedString = NSMutableAttributedString(string: string)
textView.attributedText = attributedString
Run Code Online (Sandbox Code Playgroud)
从这里我知道我需要找到需要更改textColor的单词范围,然后将它们添加到属性字符串中.我需要知道的是如何从attributionString中找到正确的字符串,然后更改它们的textColor.
由于我的评分太低,我无法回答我自己的问题,但这是我找到的答案
我通过翻译来翻译一些代码来找到我自己的答案
以下是Swift中的实现示例:
let string = "A \(stringOne) and \(stringTwo)"
var attributedString = NSMutableAttributedString(string:string)
let stringOneRegex = NSRegularExpression(pattern: nameString, options: nil, error: nil)
let stringOneMatches = stringOneRegex.matchesInString(longString, options: nil, range: NSMakeRange(0, attributedString.length))
for stringOneMatch in stringOneMatches {
let wordRange = stringOneMatch.rangeAtIndex(0)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.nameColor(), range: wordRange)
}
textView.attributedText = attributedString
Run Code Online (Sandbox Code Playgroud)
由于我想要更改多个字符串的textColor,我将创建一个帮助函数来处理它,但这适用于更改textColor.
我有这样的字符串
var str = "@text1 this is good @text1"
Run Code Online (Sandbox Code Playgroud)
现在换成text1另一个字符串,比方说t 1.我能够替换文本,但我无法加粗.我想加粗新字符串t 1,以便最终输出为:
@t 1这是好的@t 1
我该怎么做?
我看到的所有示例都在Objective-C中,但我想在Swift中进行.
提前致谢.
我有以下代码,但我的链接始终是蓝色的.我如何塑造它们的颜色?
[_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)];
Run Code Online (Sandbox Code Playgroud)
_string是一个NSMutableAttributedString,位置和长度工作正常.
我尝试为字符串的一部分加下划线,例如,' test string '字符串中的' string '部分.我正在使用,我的解决方案运作良好.NSMutableAttributedStringiOS7
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]
initWithString:@"test string"];
[attributedString addAttribute:NSUnderlineStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:NSMakeRange(5, 6)];
myLabel.attributedText = attributedString;
Run Code Online (Sandbox Code Playgroud)
问题是我的解决方案iOS8不再适用了.在花了一个小时测试多个变体之后NSMutableAttributedString,我发现这个解决方案只在范围从0开始时才有效(长度可以不同).这是什么原因?我该如何解决这个问题?
我创建了一个采用NSAttributedString的方法,我正在寻找动态创建子视图和标签以将字符串放入.因为需要确定字体和大小等属性来正确确定标签的大小,我需要确定是否可以迭代已应用于属性字符串的值和范围?
我知道我可以单独传递属性,但为了可重用性,我希望能够尽可能少地将参数传递给方法.
我们可以不转换NSMutableAttributedString成NSString?
我有两个NSMutableAttributedStrings,我将第二个字符串附加到1st,如下所示:
[string1 appendAttributedString:string2];
因为我必须在标签上显示string1:
self.label1.text = (NSString *)string1;
Run Code Online (Sandbox Code Playgroud)
我收到了"unrecognized selector sent to instance"错误.
我在这里做错了吗?这不是分配NSMutableAttributedString标签的文本属性的正确方法吗?
我有一个,NSMutableAttributedString并希望将其转换回简单String.
var attributedString = NSMutableAttributedString(string: "hello w0rld")
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到公正的String走出了一条NSMutableAttributedString最简单的?
我找到了几个崩溃报告的原因unexpected start state.我的代码看起来像这样:
NSRange range = [content rangeOfString:@"<html>"];
if (range.location != NSNotFound) {
NSString *htmlStr = [content substringFromIndex:range.location];
NSAttributedString *attStr = [[NSAttributedString alloc] initWithData:[htmlStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
return attStr.string;
}
Run Code Online (Sandbox Code Playgroud)
崩溃报告如下所示:
我觉得我错过了一些简单的东西,但我似乎无法找到如何做到这一点:
我将属性设置为如下链接:
[myAttrString addAttribute:NSLinkAttributeName value:linkURL range:selectedRange];
Run Code Online (Sandbox Code Playgroud)
这是有效的,但链接是蓝色的,我似乎无法改变颜色.这将除了链接之外的所有内容设置为白色
[myAttrString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:selectedRange];
Run Code Online (Sandbox Code Playgroud)
是否有其他颜色属性名称,我似乎无法找到特定于链接?
我正在尝试在我的Swift应用程序中为某些文本添加下划线.这是我目前的代码:
let text = NSMutableAttributedString(string: self.currentHome.name)
let attrs = [NSUnderlineStyleAttributeName:NSUnderlineStyle.PatternDash]
text.addAttributes(attrs, range: NSMakeRange(0, text.length))
homeLabel.attributedText = text
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误text.addAttributes:
NSString与...不完全相同NSObject
如何将枚举中包含的属性添加到Swift中的NSMutableAttributedString?
ios ×9
swift ×4
ios8 ×2
iphone ×2
objective-c ×2
uilabel ×2
crash ×1
nsexception ×1
nsstring ×1
string ×1