トップ 最新 追記

Cocoa練習帳

iOS/iPhone/iPad/watchOS/tvOS/MacOSX/Android プログラミング, Objective-C, Cocoa, Swiftなど

2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|03|

2022-09-09 [Web3.0][Ethereum][macOS]プライベート・ネットワーク

Ethereum関連の開発を進めるにあたってローカル環境にプライベート・ネットワークが必要になるので、その手順を残す。

HomebrewでGeth (Go Ethereum) をインストールする。

$ brew tap ethereum/ethereum
$ brew install ethereum

データ・ディレクトリを作成する。

$ cd ${PATHTO}
$ mkdir eth_private_net
$ cd eth_private_net

Genesisファイル myGenesis.json を作成する。

{
  "config": {
    "chainId": 15,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0,
    "berlinBlock": 0
  },
  "nonce": "0x0000000000000042",
  "timestamp": "0x0",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "extraData": "",
  "gasLimit": "0x8000000",
  "difficulty": "0x4000",
  "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "coinbase": "0x3333333333333333333333333333333333333333",
  "alloc": {}
}

genesisブロックを初期化する。

$  geth --datadir ${PATHTO}/eth_private_net init ${PATHTO}/eth_private_net/myGenesis.json

gethを起動する。

$ geth --networkid "15" --nodiscover --datadir "${PATHTO}/eth_private_net" console

このプライベート・ネットワークにアカウントを作成し、スマート・コントラクトを実行させれば開発を進められるが、アカウントやスマートコントラクトのアドレスを覚えておくなど管理が手間となるので、Truffle Suite (TruffleとGanache) の利用をお勧めする。

Truffleをインストールする。

$ npm install -g truffle

Ganacheをダウンロードする。

ダウンロードした Ganache-?.?.?-mac.dmg をダブルクリックして、Ganacheアプリをインストールする。

_ ソースコード

GitHubからどうぞ。
https://github.com/murakami/workbook/tree/master/blockchain/eth_private_net - GitHub

2022-09-12 [macOS][Cocoa][Swift]ハイパー・リンクを埋め込む

Cocoaのテキストを扱うUI部品には高度な機能が用意されているということで、今回はハイパー・リンクの埋め込みを試してみた。

拡張(extension)を使ってNSAttributedStringにハイパー・リンクの埋め込み機能を追加する。

import Cocoa
 
extension NSAttributedString {
    class func hyperlink(inString: String, aURL: NSURL) -> NSAttributedString {
        let attrString = NSMutableAttributedString(string: inString)
        let range = NSMakeRange(0, attrString.length)
        
        attrString.beginEditing()
        attrString.addAttribute(.link, value: aURL.absoluteString ?? "", range: range)
        
        /* make the text appear in blue */
        attrString.addAttribute(.foregroundColor, value: NSColor.blue, range:range)
        
        /* next make the text appear with an underline */
        attrString.addAttribute(.underlineStyle, value:NSNumber(value: NSUnderlineStyle.single.rawValue), range:range)
        
        attrString.endEditing()
        
        return attrString
    }
}

NSTextFieldにハイパー・リンクが埋め込まれた文字列を設定する。

    private func setHyperlink(inTextField: NSTextField) {
        // both are needed, otherwise hyperlink won't accept mousedown
        inTextField.allowsEditingTextAttributes = true
        inTextField.isSelectable = true
        
        if let url = NSURL(string: "http://www.bitz.co.jp/") {
            let string = NSMutableAttributedString()
            string.append(NSAttributedString.hyperlink(inString: "Bitz Co., Ltd.", aURL: url))
            
            // set the attributed string to the NSTextField
            inTextField.attributedStringValue = string
        }
    }

NSTextViewにハイパー・リンクが埋め込まれた文字列を設定する。

    private func setHyperlink(inTextView: NSTextView) {
        // create the attributed string
        let string = NSMutableAttributedString()
        
            // create the url and use it for our attributed string
        if let url = NSURL(string: "http://www.bitz.co.jp/") {
            string.append(NSAttributedString.hyperlink(inString: "Bitz Co., Ltd.", aURL: url))
            
            // apply it to the NSTextView's text storage
            inTextView.textStorage?.setAttributedString(string)
        }
    }

NSTextFieldとNSTextViewの内部の構成の違いから、NSTextViewはNSTextStorageに対してハイパー・リンク付き文字列を設定する。

_ ソースコード

GitHubからどうぞ。
https://github.com/murakami/workbook/tree/master/mac/EmbeddingHyperlinks - GitHub

トップ 最新 追記