macos - 如何将NSString写入NSView?

标签 macos cocoa nsstring nsview

我可以使用类似的东西(UI 而不是 NS)写入 iOS。但在 OSX 上失败了?

NSString *sText = @"Hello";
[sText drawInRect:CGRectMake(x, y, 150, 20) withFont:[NSFont boldSystemFontOfSize:fSize]]; 
// No visible @interface for 'NSString' declares the selector 'drawInRect:withFont:'

最佳答案

您需要在 OS X 上使用 NSAttributedString 属性:

NSString *sText = @"Hello";
[sText drawInRect:CGRectMake(x, y, 150, 20) withAttributes: @{ NSFontAttributeName : 
                                             [NSFont boldSystemFontOfSize:fSize] }];

https://developer.apple.com/library/prerelease/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSString_AppKitAdditions/index.html

关于macos - 如何将NSString写入NSView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35323407/

相关文章:

iphone - 在大数组中搜索字符串需要很长时间

java - OSX : JavaVM, AWT/Swing 和可能的死锁

cocoa - 有没有办法让 NSAnimation "Begin from current state"就像在 iPhone 上一样?

cocoa - ViewController 与 View

objective-c - 创建一个类似 Safari 的后退/前进按钮?

objective-c - [__NSCFSet stringByAppendingFormat :]: unrecognized selector sent to instance

mac/eclipse 上的 c++ 在崩溃时不显示错误

list - SwiftUI 列表行填充宽度

c++ - Macports GCC 4.8 无法链接 OSX Lion 上的 c++ 库

cocoa - 将 NSArray 重新格式化为典型的 NSString