小编clo*_*xnu的帖子

Xcode 9 中的 Swift 4 - 如何在 NSAttributedstring 中使用 boundingrect?

我想获取 UILabel 中某些文本的长度,并动态更改 UILabel 的 width 属性。

但是我不知道如何在函数boundingrect中设置参数。这是 Apple Developer 的文档。

func boundingRect(with size: CGSize, 
      options: NSStringDrawingOptions = [], 
      context: NSStringDrawingContext?) -> CGRect
Run Code Online (Sandbox Code Playgroud)

,我试过这样

let attr = NSMutableAttributedString(string: "test test test test , this is test text. test test test test , this is test text. test test test test , this is test text. ")
//attr.addattributes
print(attr.boundingrect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: 0), option: .truncatesLastVisibleLine
,context: nil)!)
Run Code Online (Sandbox Code Playgroud)

但最后我在打印时得到了错误的宽度,那么为什么以及如何获得正确的宽度。

nsattributedstring ios swift swift4

2
推荐指数
1
解决办法
5433
查看次数

如何在 swift 中向 NSArray 添加 nil 值?

NSArray.init(数组: [任意])

NSArray 有一个初始函数,其参数为 [Any]。但不是[Any?],那么,如何给这个数组添加一个nil值呢?

swift swift4

2
推荐指数
1
解决办法
872
查看次数

Xcode 9 中的 Swift 4 - 如何轻量级核心数据迁移?

我的核心数据将再更新一个属性,为了避免崩溃,我首先添加了一个新的模型版本,此外:

关于这个问题的大部分关键是改变:

coordinator!.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)
Run Code Online (Sandbox Code Playgroud)

选项:在代码中为零

options:[NSMigratePersistentStoresAutomaticallyOption:true, NSInferMappingModelAutomaticallyOption: true]
Run Code Online (Sandbox Code Playgroud)

但是在我的appdelegate.swift 中,我找不到任何“persistentStoreCoordinator”,那么我可以在我的版本中迁移 CoreData 吗?

xcode core-data swift swift4 ios11

1
推荐指数
2
解决办法
1655
查看次数

标签 统计

swift ×3

swift4 ×3

core-data ×1

ios ×1

ios11 ×1

nsattributedstring ×1

xcode ×1