我有一个带有NSTextView控件的 Cocoa 应用程序,该控件将其文本保存在一个NSAttributedString(实际上我相信它是一个NSMutableAttributedString)中。我可以轻松地在该字符串内的不同字符范围内设置和修改不同的文本属性(例如字体、下划线等)。
但是,我想将文本的一部分设置为隐藏(类似于 CSS 属性的效果display: none)。当外部事件发生时(比如单击按钮),我想取消隐藏或隐藏该特定范围的字符。
有没有办法做到这一点NSAttributedString?
我有一个自定义视图NSMenuItem包含一个NSButton.按钮的标题是在运行时确定的,我想调整按钮的框架,以便内容(按钮标题)始终适合.
我知道[NSAttributedString boundingRectWithSize:options:],但我想知道是否有一个内置的方法来为NSButtons甚至在NSView级别上执行此操作.
我怎样才能创建一个基本上是水平分隔符的NSAttributedString,比如HTML <hr>标签?
我试过了:
NSAttributedString *hr = [[NSAttributedString alloc] initWithHTML:
[NSData
dataWithBytes:"<html><body><hr>hi</body></html>"
length:strlen("<html><body><hr>hi</body></html>")]
documentAttributes:NULL];
Run Code Online (Sandbox Code Playgroud)
但是,<hr>标签不会出现.将字符串添加到我的视图可以正常工作,因为我可以看到hi.
我在这篇文章中找到了NSAttributedString的例子.我的问题是 - 是否有任何提示,如何使用它UIRefreshControl class?
从上面的帖子:
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"firstsecondthird"];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5,6)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(11,5)];
Run Code Online (Sandbox Code Playgroud)
属性是否UIRefreshControl自动调用?
编辑:
我知道如何设置它,我想知道它是否用于任何其他目的然后"只是"格式化标签 - 是否能够显示两个字符串的UIRefresherControl ?一个被拉出之前,一个被拉了之后?当我看到我不能放入"普通"字符串时,这就是我的想法.

@interface ViewController ()<UITextViewDelegate>
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"www.google.com"];
NSDictionary *linkDic = @{ NSLinkAttributeName : [NSURL URLWithString:@"http://www.google.com"] };
[str setAttributes:linkDic range:[[str string] rangeOfString:@"www.google.com"]];
_textView.attributedText = str;
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
NSLog(@"=============%@",URL);
return YES;
}
Run Code Online (Sandbox Code Playgroud)
有什么不对?
Swift 2.0新手; 试图学习它,对动画感兴趣并尝试编写一些代码,一次在屏幕上显示一个消息.
我写了这个,它有效,但是我忍不住可以不用CALayers和/或alpha值做些什么?或者一些有生命的小发明,更快捷的东西; 这种感觉和看起来有点笨重,真的很好.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let supertext = "A long time ago, in a galaxy far, far away ..."
let round = supertext.characters.count
for i in 0...round {
let delay = Double(i)/10
let subtext = supertext[supertext.startIndex..<supertext.startIndex.advancedBy(i)]
NSTimer.scheduledTimerWithTimeInterval(delay, target: self, selector: "ghostText:", userInfo: ["theText" :subtext], repeats: false)
}
}
var view3:UITextView = UITextView()
func ghostText(timer:NSTimer) {
view3.removeFromSuperview() …Run Code Online (Sandbox Code Playgroud) 我的问题在理论上与此类似:UIButton上的iOS NSAttributedString
我希望将我的按钮的标题读为:
"带下划线的字符串
一些文字"
这需要以swift和100%以编程方式完成.
我试图通过使用NSMutableAttributedString创建带下划线的部分,然后将其附加到其他文本(使用换行符引导)来尝试这样做.但是,这给了我错误"无法分配类型'Void'('aka'()')的值来键入'NSMutableAttributedString"
代码如下:
var patientName = NSMutableAttributedString(string:"Patient Name", attributes: underlineAttributes)
var clickforinfomessage = NSMutableAttributedString(string: "\nclick for patient info")
clickforinfomessage = clickforinfomessage.appendAttributedString(patientName)
startVisitButton.setAttributedTitle(clickforinfomessage, forState: .Normal)
Run Code Online (Sandbox Code Playgroud) 我有一个UITextView带有一些属性文本的文本,其中textContainer.maximumNumberOfLine设置了(在这种情况下为 3)。
我想在属性字符串的字符范围内找到省略号字符的索引。
例如:
原始字符串:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit"
截断后显示的字符串:
Lorem ipsum
dolor sit amet,
consectetur...
我如何确定 的索引...?
我在IB中创建了许多UILabel,它们都归因于文本.每个标签的文本包含多行不同的字体大小和颜色.
在运行时,我希望能够只更改这些标签的字体名称,而无需更改现有的字体大小或颜色.
我已经研究过,无法找到一种直接的方法来实现这一目标.有任何想法吗?
我正在使用 html 创建 NSAttributedString:
let htmlString = "<body style='padding-left:50px'><h1>Hello World</h1><div><a href=https://apple.com/offer/samsung-faq/>Click Here</a></div><p>This is a sample text</p><pre>This is also sample pre text</pre></body>"
Run Code Online (Sandbox Code Playgroud)
在这里我使用扩展方法将其设置为 UILabel
someLabel.attributedText = htmlString.htmlToAttributedString
Run Code Online (Sandbox Code Playgroud)
NSAttributedString 扩展:
extension String {
var htmlToAttributedString: NSAttributedString? {
guard let data = data(using: .utf8) else { return NSAttributedString() }
do {
return try NSAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
} catch {
return NSAttributedString()
}
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我想要一个回调方法来检测 html 字符串中作为锚标记存在的链接。我将如何获得点击事件以及如何获得该事件回调中的网址?
请帮忙...