NSTextView 研究(1)

前端之家收集整理的这篇文章主要介绍了NSTextView 研究(1)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这次研究的目标是NSTextView 控件的文本处理图片处理问题

这是一个初步演示,在这里我们建立一个类来处理文本的写入问题。你建立一个Cocoa工程,然后在View里拖入一个NSTextView对象,并在

ViewController类中添加 textView 变量。然后在viewDidLoad() 函数添加代码。令人你需要找一个girl.jpg文件,也拖入工程。

用到的代码全部列在后面,就不解释了。



里面添加代码

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        let attachmentCell = NSTextAttachmentCell(imageCell: NSImage(named: "girl.jpg"))
        let attachment = NSTextAttachment()
        attachment.attachmentCell = attachmentCell
        
        let attributedStr = NSAttributedString(attachment: attachment)
        self.textView.textStorage?.insert(attributedStr,at: 0)

         let designer = TextAttributedDesigner("Designer Example")
         designer.newline()
         designer.append("ForegroundColor",attrType: .foregroundColor(NSColor.blue))
         designer.newline()
         designer.append("BackgroundColor",attrType: .backgroundColor(NSColor.lightGray))
         designer.newline()
         designer.append("Font (Cochin-BoldItalic)",attrType: .font(NSFont(name: "Cochin-BoldItalic",size: 30)!))
         designer.newline()
         let para = NSMutableParagraphStyle()
         para.alignment = .center
         designer.append("this is paragraph text,the text is so length over than normal text line.",attrType: AttributeItemType.paragraphStyle(para))
         designer.newline()
         
         let para1 = NSMutableParagraphStyle()
         para1.firstLineHeadIndent = 20
         designer.append("this is left indent text,the text is so length than normal text line.this is left indent text,the text is so length over than normal text line.this is backgroundColor indent text,attrType: AttributeItemType.paragraphStyle(para1))
         designer.newline()
         designer.append("ligature",attrType: .ligature(0))
         designer.newline()
         designer.append("the kern is long.",attrType: .kern(4))
         designer.newline()
         designer.append("strikethrough",attrType: .strikethrough(4))
         designer.newline()
         designer.append("strikethrough",attrType: .strikethrough(3))
         designer.newline()
         designer.append("strikethrough",attrType: .strikethrough(2))
         designer.newline()
         designer.append("strikethrough",attrType: .strikethrough(1))
         designer.newline()
         designer.append("strikethrough",attrType: .strikethrough(0))
         designer.newline()
         designer.append("strikethrough",attrType: .strikethrough(9))
         designer.newline()
         
         designer.append("underline",attrType: .underline(5))
         designer.newline()
         designer.append("underline",attrType: .underline(3))
         designer.newline()
         designer.append("underline",attrType: .underline(1))
         designer.newline()
         designer.append("underline",attrType: .underline(0))
         designer.newline()
         designer.append("StrokeColor",attrType: .strokeColor(NSColor.blue))
         designer.newline()
         
         let shdw = NSShadow()
         shdw.shadowColor = NSColor.brown
         shdw.shadowBlurRadius = 3
         shdw.shadowOffset = NSMakeSize(3,-3)
         designer.append("Shadow",attrType: .shadow(shdw))
         designer.newline()
         designer.append("TextEffect",attrType: .textEffect(NSTextEffectLetterpressStyle as NSString))
         designer.newline()
         
         designer.append(" Many Font (Cochin-BoldItalic)",attrTypes: [ .font(NSFont(name: "Cochin-BoldItalic",size: 30)!),.strokeColor(NSColor.blue),.strokeWidth(0.5)])
         designer.newline()
         designer.newline()
         
         // 如果想有外框效果。
         designer.append("OUTLINE",attrTypes: [.strokeColor(NSColor.blue),.strokeWidth(1) ])
         designer.newline()
        
         self.textView.insertText( designer.attributedString,replacementRange: NSMakeRange(0,0))
    }


支持上面写法的一个文本支持支持类:
//
//  TextAttributedDesigner.swift
//  TestTextKit
//
//  Created by wuzhiqiang on 2016/10/11.
//  Copyright © 2016年 wuzhiqiang. All rights reserved.
//

import Cocoa

public enum AttributeItemType {
    case font(NSFont)
    case paragraphStyle(NSParagraphStyle)
    case foregroundColor(NSColor)
    case backgroundColor(NSColor)
    case ligature(NSNumber)             // 是否使用连字符
    case kern(NSNumber)                 // 字距调整
    case strikethrough(NSNumber)
    case underline(NSNumber)
    case strokeColor(NSColor)
    case strokeWidth(NSNumber)
    case shadow(NSShadow)
    case textEffect(NSString)
    case attachment(NSTextAttachment)
    case link(NSURL)
    case baselineOffset(NSNumber)
    case obliqueness(NSNumber)
    case expansion(NSNumber)
    case writingDirection([NSNumber])
    case verticalGlyphForm(NSNumber)
    case markedClauseSegment(NSNumber)
    case textAlternatives(NSTextAlternatives)
    case spellingState(NSNumber)
    case cursor(NSNumber)
    case toolTip(NSString?)
    
    public var key: String {
        switch self {
        case .font: return NSFontAttributeName
        case .paragraphStyle: return NSParagraphStyleAttributeName
        case .foregroundColor: return NSForegroundColorAttributeName
        case .backgroundColor: return NSBackgroundColorAttributeName
        case .ligature: return NSLigatureAttributeName
        case .kern: return NSKernAttributeName
        case .strikethrough: return NSStrikethroughStyleAttributeName
        case .underline: return NSUnderlineStyleAttributeName
        case .strokeColor: return NSStrokeColorAttributeName
        case .strokeWidth: return NSStrokeWidthAttributeName
        case .shadow: return  NSShadowAttributeName
        case .textEffect: return NSTextEffectAttributeName
        case .attachment: return NSAttachmentAttributeName
        case .link: return NSLinkAttributeName
        case .baselineOffset: return NSBaselineOffsetAttributeName
        case .obliqueness: return NSObliquenessAttributeName
        case .expansion: return NSExpansionAttributeName
        case .writingDirection: return NSWritingDirectionAttributeName
        case .verticalGlyphForm: return NSVerticalGlyphFormAttributeName
        case .markedClauseSegment: return NSMarkedClauseSegmentAttributeName
        case .textAlternatives: return NSTextAlternativesAttributeName
        case .spellingState: return NSSpellingStateAttributeName
        case .cursor: return NSCursorAttributeName
        case .toolTip: return NSToolTipAttributeName
        }
    }
    public var keyValuePair: [String: Any] {
        switch self {
        case .font(let value): return [self.key : value]
        case .paragraphStyle(let value): return [self.key : value]
        case .foregroundColor(let value): return [self.key : value]
        case .backgroundColor(let value): return [self.key : value]
        case .ligature(let value): return [self.key : value]
        case .kern(let value): return [self.key : value]
        case .strikethrough(let value): return [self.key : value]
        case .underline(let value): return [self.key : value]
        case .strokeColor(let value): return [self.key : value]
        case .strokeWidth(let value): return [self.key : value]
        case .shadow(let value): return [self.key : value]
        case .textEffect(let value): return [self.key : value]
        case .attachment(let value): return [self.key : value]
        case .link(let value): return [self.key : value]
        case .baselineOffset(let value): return [self.key : value]
        case .obliqueness(let value): return [self.key : value]
        case .expansion(let value): return [self.key : value]
        case .writingDirection(let value): return [self.key : value]
        case .verticalGlyphForm(let value): return [self.key : value]
        case .markedClauseSegment(let value): return [self.key : value]
        case .textAlternatives(let value): return [self.key : value]
        case .spellingState(let value): return [self.key : value]
        case .cursor(let value): return [self.key : value]
        case .toolTip(let value): return [self.key : value]
        }
    }
}

public struct TextAttributedDesigner {
    internal let attrText = NSMutableAttributedString()
    
    public var attributedString: NSAttributedString {
        return attrText
    }
    // Texkit Support
    public init(_ text: String,attrType: AttributeItemType? = nil ){
        self.append(text,attrType: attrType )
    }
    public func newline(attrType: AttributeItemType? = nil){
        self.append( "\r\n",attrType: attrType )
    }
    public func append(_ text: String,attrType: AttributeItemType? = nil ){
        let attr =  NSAttributedString(string: text,attributes: attrType?.keyValuePair )
        self.attrText.append( attr )
    }
    public func insert(_ text: String,at loc: Int,attributes: attrType?.keyValuePair )
        self.attrText.insert( attr,at: loc)
    }
    public func append(_ text: String,attrTypes: [AttributeItemType] ){
        let attr =  NSAttributedString(string: text,attributes: ItemTypesToDict(attrTypes: attrTypes) )
        self.attrText.append( attr )
    }
    public func insert(_ text: String,attributes: ItemTypesToDict(attrTypes: attrTypes) )
        self.attrText.insert( attr,at: loc)
    }
    
    //////////
    private func ItemTypesToDict(attrTypes: [AttributeItemType] )->[String: Any] {
        var dict = [String:Any]()
        for attr in attrTypes {
            let keyValue = attr.keyValuePair
            for (key,value) in keyValue {
                dict[key] = value
            }
        }
        return dict
    }

}


这里的代码简化了NSTextView的文本和图片的插入。
原文链接:https://www.f2er.com/swift/322848.html

猜你在找的Swift相关文章