我相信我理解如何设置NSAttributedString值,但实际上你是如何在界面中显示它们的呢?
示例:UILabel,UITextView等
非常感谢具体说明/
我正在尝试允许将不同样式的文本输入到UITextView中,有点像文本编辑器,使用粗体或斜体等简单属性.我理解通过使用textView的attributedText属性,我可以将属性应用于特定范围的文本.这很好,但我希望能够将属性文本键入textView,它将通过按钮切换(例如输入粗体文本).
这是我到目前为止所想到的:
我已经使用-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)textUITextView委托方法获取text参数,并通过创建具有相同文本的NSAttributedString来使用属性对其进行修改.然后创建一个NSMutableAttributedString,它是originText的副本textView.使用appendAttributedString,添加两个,然后将textView的attributedText属性设置为生成的attributionString.
这是代码:
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if (self.boldPressed) {
UIFont *boldFont = [UIFont boldSystemFontOfSize:self.textView.font.pointSize];
NSDictionary *boldAttr = [NSDictionary dictionaryWithObject:boldFont forKey:NSFontAttributeName];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]initWithString:text attributes:boldAttr];
NSMutableAttributedString *textViewText = [[NSMutableAttributedString alloc]initWithAttributedString:textView.attributedText];
[textViewText appendAttributedString:attributedText];
textView.attributedText = textViewText;
return NO;
}
return YES;
}
Run Code Online (Sandbox Code Playgroud)
每次输入一个字符时都必须重置textViews attributionText似乎对于一个简单的操作有点多.不仅如此,它还不能正常工作.以下是启用粗体属性时的样子:

这有两个问题.最明显的是每个新角色如何被置于一条新线上.但同样奇怪的是插入点始终位于textview中文本的第一个索引处(仅当启用粗体时,粗体字符才会插入新行).因此,如果您使用粗体启用键入,然后关闭加粗,请在所有现有文本前键入简历.
我不确定为什么会发生这些错误.我也不认为我的解决方案非常有效,但我想不出任何其他实现方法.
我想显示3行NSAttributedString.有没有办法根据宽度和线数找出所需的高度?
而且我不想创建UILabel来进行大小计算,因为我希望计算在后台线程中完成.
如何将所有属性设置NSMutableAttributedString为空?你必须通过它们进行枚举并删除它们吗?
我不想创建一个新的.我工作的textStorage在NSTextView.设置新字符串会重置光标位置NSTextView并触发委托.
通常我在界面构建器中构建app界面.有时设计需要使用属性字符串(字体,颜色等).如果string是静态的,则很容易配置.
但是如果string是动态的(带参数的格式),则无法在界面构建器中配置属性.它需要编写大量代码.
我找的一些类似物[NSString stringWithFormat:]的NSAttributedString.所以我将能够在界面构建器中设置字符串格式和必要的属性,然后在代码中提供必要的参数.
例如:
让我们考虑我需要这样格式的显示字符串:" %d + %d = %d "(所有数字都是粗体).
我想在界面构建器中配置此格式.在代码中我想提供参数:1,1,2.应该显示" 1 + 1 = 2 ".
我有一个UILabel,文本在故事板中设置为"归属".当我生成Main.strings文件以翻译成另一种语言时,不会出现此标签的文本.
我试图通过复制对象id手动将一个条目添加到Main.strings文件中.我尝试设置"text"属性和"attributedText"属性,但是当我运行应用程序时,不使用已翻译的字符串.
那么,如何将UILabel集本地化为故事板上的"归属"?
我有一个问题,"boundingRectForGlyphRange"总是返回CGRect.zero"0.0,0.0,0.0,0.0"."boundingRectForGlyphRange"无效.例如,我正在编写触摸UILabel功能的部分文本.我的文字的第一部分是"任何文字",第二部分是"阅读更多".我希望点击识别器仅在我触摸"READ MORE"时才能工作.如果我触摸UILabel上的任何一点,"CGRectContainsPoint"总是返回true,然后调用该动作
这是我的代码:
override func viewDidLoad() {
super.viewDidLoad()
// The full string
let firstPart:NSMutableAttributedString = NSMutableAttributedString(string: "Lorem ipsum dolor set amit ", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(13)])
firstPart.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(),
range: NSRange(location: 0, length: firstPart.length))
info.appendAttributedString(firstPart)
// The "Read More" string that should be touchable
let secondPart:NSMutableAttributedString = NSMutableAttributedString(string: "READ MORE", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(14)])
secondPart.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(),
range: NSRange(location: 0, length: secondPart.length))
info.appendAttributedString(secondPart)
lblTest.attributedText = info
// Store range of chars we want to detect touches for
moreStringRange = NSMakeRange(firstPart.length, secondPart.length)
print("moreStringRange\(moreStringRange)") …Run Code Online (Sandbox Code Playgroud) 在创建一个NSAttributedStringHTML时,使用NSHTMLTextDocumentType,我发现\n即使在最后一段之后也会为每个段落添加一个.这是在文本的最后一段下面添加了不需要的填充UILabel.如何仅删除最后一段的额外填充?
NSString *style = @"<style> body { font-family: Avenir; font-size: 18px; color: blue; } p:last-of-type { margin: 0; }</style>";
NSString *html = @"<p>A whole bunch of sample text goes right here.</p><p>Now here's another paragraph that unfortunately has an extra line underneath the text adding undesired padding to the label. :(</p>";
NSString *styledHtml = [NSString stringWithFormat:@"%@%@", style, html];
self.label.attributedText = [[NSMutableAttributedString alloc] initWithData:[styledHtml dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
Run Code Online (Sandbox Code Playgroud)
我WKInterfaceLabel在运行watchOS 5的Apple Watch应用程序中有一个.我将其属性字符串设置为包含应用于子字符串的粗体字体属性的字符串.运行应用程序时,粗体显示正确.但是,如果您在iPhone上的Watch应用程序中更改动态类型的大小,则手表上的文本大小会更改,但粗体消失.
我希望watchOS只是设置WKInterfaceLabel消除粗体字体的字体.保留其他属性,例如前景色.
我注意到NSNotification.Name.didChangeNotificationWatchKit不支持,所以我不能拦截这个大小的变化.
如何处理WatchKit中的动态类型更改?有没有办法在用户更改文本大小时保留属性字符串中的字体属性?
nsattributedstring swift apple-watch watchkit wkinterfacelabel
我正在实施“在聊天中搜索”功能,并且希望在消息中突出显示搜索到的单词。如标题所述,问题在于如果单词是一行的第一行(长消息显然是多行的),则整行而不是单个单词会被突出显示。
在调试时,我也尝试应用下划线而不是背景色,下划线是正确的。我无法弄清楚问题出在哪里。我的应用程序的聊天基于JSQMessagesViewController,所以我认为问题可能出在那里。
[attributedString addAttribute:NSBackgroundColorAttributeName
value:backColor
range:wordRange];
[attributedString addAttribute:NSUnderlineStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:wordRange];
cell.textView.attributedText = attributedString;
Run Code Online (Sandbox Code Playgroud)
我不发布计算范围的代码,因为范围是正确的;事实上,如果我在调试中查看 attributedString 的预览(我将它分配给 cell.textView.attributedText),我可以看到只有单词被突出显示,而不是所有字符串。
objective-c nstextview nsattributedstring ios jsqmessagesviewcontroller
ios ×7
objective-c ×4
swift ×4
apple-watch ×1
iphone ×1
localization ×1
nstextview ×1
uilabel ×1
uitextview ×1
watchkit ×1