Mac OS X上的NSTextContainer有一个方法replaceLayoutManager:用NSLayoutManager的子类替换NSTextView的NSLayoutManager。
不幸的是iOS没有这样的功能。我尝试了这些代码行的组合,但它不断崩溃。
THLayoutManager *layoutManager = [[THLayoutManager alloc] init];
[layoutManager addTextContainer:[self textContainer]];
// [[self textStorage] removeLayoutManager:[self layoutManager]];
//[[self textStorage] addLayoutManager:layoutManager];
[[self textContainer] setLayoutManager:layoutManager];
Run Code Online (Sandbox Code Playgroud)
替换 UITextview 的 NSLayoutManager 的正确步骤是什么?
Swift允许您定义枚举,但核心数据不支持(开箱即用)如何保存它们.
我在互联网上看到的推荐解决方案(到目前为止使用)是使用私有变量:
class ManagedObjectSubClass : NSManagedObject
{
enum Cards : Int
{
case Diamonds, Hearts
}
@nsmanaged var cardRaw: Int
var card : Cards {
set { self.cardRaw = newValue.rawValue }
get { return Cards(RawValue:cardRaw)! }
}
}
Run Code Online (Sandbox Code Playgroud)
在下面的答案中给出了另一种解决方案.
随着NSMutableData我可以创建数组Int's或Float的和存储的磁盘.
protocol BinaryConvertible
{
init()
}
extension Int : BinaryConvertible {}
struct Storage<T: BinaryConvertible>
{
let data = NSMutableData()
func append(value: T)
{
var input = value
data.append(&input, length: sizeof(T))
}
func extract(index: Int) -> T
{
var output = T()
let range = NSRange(location: index * sizeof(T), length: sizeof(T))
data.getBytes(&output, range: range)
return output
}
}
Run Code Online (Sandbox Code Playgroud)
Swift 3有一种在引擎盖下Data使用的新型NSData.喜欢String和NSString.我无法弄清楚如何添加例如Double使用新方法.
append函数现在需要a UnsafePointer<UInt8>,但是你如何从一个Double或任何随机结构创建它?
此片段可用于使用CGContext绘制CGGlyphs.
//drawing
let coreGraphicsFont = CTFontCopyGraphicsFont(coreTextFont, nil)
CGContextSetFont(context, coreGraphicsFont);
CGContextSetFontSize(context, CTFontGetSize(coreTextFont))
CGContextSetFillColorWithColor(context, Color.blueColor().CGColor)
CGContextShowGlyphsAtPositions(context, glyphs, positions, length)
Run Code Online (Sandbox Code Playgroud)
但是如何从包含标志符号或重音字符等表情符号的快速字符串中获取CGGlyphs?
let string = "swift: \u{1F496} \u{65}\u{301} \u{E9}\u{20DD} \u{1F1FA}\u{1F1F8}"
Run Code Online (Sandbox Code Playgroud)
这些方法都没有显示特殊字符,即使它们已正确打印到控制台.请注意,第一种方法返回NSGlyph,但绘制时需要CGGlyph.
var progress = CGPointZero
for character in string.characters
{
let glyph = font.glyphWithName(String(character))
glyphs.append(CGGlyph(glyph))
let advancement = font.advancementForGlyph(glyph)
positions.append(progress)
progress.x += advancement.width
}
Run Code Online (Sandbox Code Playgroud)
或者这第二种方法需要转换为NSString:
var buffer = Array<unichar>(count: length, repeatedValue: 0)
let range = NSRange(location: 0, length: length)
(string as NSString).getCharacters(&buffer, range: range)
glyphs = Array<CGGlyph>(count: length, repeatedValue: 0)
CTFontGetGlyphsForCharacters(coreTextFont, &buffer, &glyphs, length) …Run Code Online (Sandbox Code Playgroud) Apple将NSPathControl改为与Yosemite中的NSPathControlItem一起使用.
但是从我所在的地方来看,这些新课程根本不起作用.我试图在我的数据结构中显示自定义路径,但我有一个常规文件路径的类似问题.是我还是Apple?
这是我的代码:
第一个代码段工作,它将显示一个路径.但这就是一切有效.
//MARK: notifications
func selectionDidChange(notification : NSNotification)
{
if let item = notification.object as? Group
{
//get "path" components
var components : [String] = [item.title ?? "a"]
var ancestor : Group? = item.parent
while (ancestor != nil)
{
components.append(ancestor?.title ?? "b")
ancestor = ancestor?.parent
}
components.append("")
//convert to url
let path = ("MyScheme:/" + "/".join(components.reverse()))
pathControl?.URL = NSURL(string: path.stringByAddingPe
}
}
Run Code Online (Sandbox Code Playgroud)
单击路径的任何部分以尝试从NSPathControlItem中获取任何属性根本不起作用.一切都归零.
@IBAction func select(sender : AnyObject)
{
println(sender.clickedPathItem??.title)
println(sender.clickedPathItem??.URL)
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用NSPathControlItem构建路径,则无法设置任何属性(title,url).
pathComponent.URL = NSURL(string: path.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!)
//let …Run Code Online (Sandbox Code Playgroud) swift ×3
cocoa ×1
core-data ×1
enums ×1
glyph ×1
ios7 ×1
nsdata ×1
osx-yosemite ×1
string ×1
swift3 ×1
typesetting ×1
uitextview ×1